Skip to content

Commit

Permalink
Feature/enable debug from local storage (#81)
Browse files Browse the repository at this point in the history
* add the local storage debug option

* update PR template

* fix integration test's container

* fix lint suggestion
  • Loading branch information
alextremp authored Nov 20, 2019
1 parent 7ff8193 commit d71834e
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 34 deletions.
33 changes: 13 additions & 20 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
# Background
## Description
<!--- Explain why this PR has to be merged, what originated this feature, ... -->

X
## Solves ticket/s
<!--- Optionally, add the tickets (jira, mantis, trello, ...) that are related to this PR -->

# Goal
## Expected behavior
<!--- Add information of what's expected to happen when this is merged -->

X
## Review steps
<!--- Nice to have, add the steps you've reproduced for a visual test in your development environment, or loc, consider adding screenshots ... -->

# Implementation

X

# Further considerations

X

# Checklist

- [ ] The PR relates to *only* one subject with a clear title.
- [ ] I have performed a self-review of my own code
- [ ] Wrote [good commit messages](http://chris.beams.io/posts/git-commit/)
- [ ] My code is readable by someone else, and I commented the hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] I have added tests that prove my fix is effective or that my feature works
## Further considerations
<!--- If applies, add information of breaking changes, agreed deploy timings, ... -->

## Memetized description
<!--- Mandatory gif, try https://giphy.com/ -->
![mandatory]()
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ For example:
http://your.web.app/page?price=5000&openads_debug
```

Also, to enable debugger in persistent mode, you can add the option in your browser's local storage from the browser's console:
```
window.localStorage.setItem('openads_debug', 'true') // removing it or setting it to false, disables the debugger
```


Currently, OpenAds uses [LogLevel](https://github.com/pimterry/loglevel) as its logging framework.


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@schibstedspain/openads",
"version": "4.1.1",
"version": "4.2.0",
"description": "OpenAds: Advertising library",
"main": "dist/",
"scripts": {
Expand Down
15 changes: 6 additions & 9 deletions src/itest/openads/infrastructure/configuration/ContainerTest.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import Container from '../../../../openads/infrastructure/configuration/Container'
import HTMLDOMDriver from '../../../../openads/infrastructure/service/HTMLDOMDriver'
import {JSDOM} from 'jsdom'

export default class ContainerTest extends Container {
constructor({config, eager = true} = {}) {
super({
config,
eager: false,
currentWindow: new HTMLDOMDriver({
dom: new JSDOM('<!DOCTYPE html><div id="forlayo">Hello world</div>')
.window
})
currentWindow: new JSDOM(
'<!DOCTYPE html><div id="forlayo">Hello world</div>',
{
url: 'http://localhost'
}
).window
})
if (eager) super._buildEagerSingletonInstances()
}

_buildDOMDriver() {
return this._currentWindow
}
}
4 changes: 4 additions & 0 deletions src/openads/domain/service/DOMDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ export default class DOMDriver {
getQueryString() {
throw new Error('DOMDriver#getQueryString must be implemented')
}

getLocalStorageValue({key}) {
throw new Error('DOMDriver#getLocalStorageValue must be implemented')
}
}
11 changes: 10 additions & 1 deletion src/openads/infrastructure/logger/LogLevelLoggerInitializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ export default class LogLevelLoggerInitializer {
return logger
}
_isDebugMode() {
return (
this._enableDebugFromLocalStorage() || this._enableDebugFromQueryString()
)
}
_enableDebugFromLocalStorage() {
return this._domDriver.getLocalStorageValue({key: DEBUG_KEY}) === 'true'
}
_enableDebugFromQueryString() {
const queryString = this._domDriver.getQueryString()
const parameters = QS.parse(queryString)
return parameters[this._loggerName.toLowerCase() + '_debug'] !== undefined
return parameters[DEBUG_KEY] !== undefined
}
_enableConnectorsDebug() {
Object.values(this._connectors).forEach(connector => {
Expand All @@ -37,3 +45,4 @@ export default class LogLevelLoggerInitializer {
})
}
}
const DEBUG_KEY = 'openads_debug'
4 changes: 4 additions & 0 deletions src/openads/infrastructure/service/HTMLDOMDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ export default class HTMLDOMDriver extends DOMDriver {
getQueryString() {
return this._dom.location.search.slice(1)
}

getLocalStorageValue({key}) {
return this._dom.defaultView.localStorage.getItem(key)
}
}
35 changes: 32 additions & 3 deletions src/test/openads/infrastructure/logger/LogLevelInitializerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import LogLevelLoggerInitializer from '../../../../openads/infrastructure/logger

describe('LogLevel Logger Initializer', () => {
const givenLoggerName = 'OpenAds'
it('Should use error as default level if no DEBUG option is in URL', () => {
it('Should use error as default level if no DEBUG option is set', () => {
const givenSearch = ''
const givenLocalStorageValue = null
const loggerMock = {
setLevel: level => null
}
const logLevelMock = {
getLogger: loggerName => loggerMock
}
const domDriverMock = {
getQueryString: () => givenSearch
getQueryString: () => givenSearch,
getLocalStorageValue: () => givenLocalStorageValue
}

const setLevelSpy = sinon.spy(loggerMock, 'setLevel')
Expand All @@ -29,14 +31,16 @@ describe('LogLevel Logger Initializer', () => {
})
it('Should use debug as level if DEBUG option is in URL, enabling debug to any connector implementing Logger interface', () => {
const givenSearch = '?a=a&openads_debug&b=b'
const givenLocalStorageValue = 'false'
const loggerMock = {
setLevel: level => null
}
const logLevelMock = {
getLogger: loggerName => loggerMock
}
const domDriverMock = {
getQueryString: () => givenSearch
getQueryString: () => givenSearch,
getLocalStorageValue: () => givenLocalStorageValue
}
const fooLoggerConnector = {
enableDebug: ({debug}) => null
Expand Down Expand Up @@ -66,4 +70,29 @@ describe('LogLevel Logger Initializer', () => {
expect(setConnectorEnableDebugSpy.calledOnce).to.be.true
expect(setConnectorEnableDebugSpy.args[0][0].debug).to.be.true
})
it('Should enable DEBUG if the openads_debug option is set in local storage', () => {
const givenSearch = ''
const givenLocalStorageValue = 'true'
const loggerMock = {
setLevel: level => null
}
const logLevelMock = {
getLogger: loggerName => loggerMock
}
const domDriverMock = {
getQueryString: () => givenSearch,
getLocalStorageValue: () => givenLocalStorageValue
}

const setLevelSpy = sinon.spy(loggerMock, 'setLevel')

const logLevelInitializer = new LogLevelLoggerInitializer({
logLevel: logLevelMock,
domDriver: domDriverMock,
loggerName: givenLoggerName
})
logLevelInitializer.logger()
expect(setLevelSpy.calledOnce).to.be.true
expect(setLevelSpy.args[0][0]).to.equal('debug')
})
})
15 changes: 15 additions & 0 deletions src/test/openads/infrastructure/service/HTMLDOMDriverTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,19 @@ describe('DOM Driver HTML simple implementation', function() {
const queryString = htmlDOMDriver.getQueryString()
expect(queryString).to.equal('')
})
it('Should get a value from the local storage', () => {
const givenDocument = new JSDOM(
'<!DOCTYPE html><div id="forlayo">Hello world</div>',
{
url: 'http://localhost'
}
).window.document
const givenKey = 'aKey'
const givenValue = 'aValue'
givenDocument.defaultView.localStorage.setItem(givenKey, givenValue)

const htmlDOMDriver = new HTMLDOMDriver({dom: givenDocument})
const result = htmlDOMDriver.getLocalStorageValue({key: givenKey})
expect(result).to.equal(givenValue)
})
})

0 comments on commit d71834e

Please sign in to comment.