Skip to content

Commit

Permalink
Rename to localCookie.js, add force cookies option.
Browse files Browse the repository at this point in the history
- Align class + filename to repository name
- Allow options to specificy whether we should force cookies (useful for
the tests).
- Update test to test localStorage and Cookies all in 1 go.
  • Loading branch information
wouterlucas committed Oct 25, 2019
1 parent 58ef2df commit e3fbd02
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 21 deletions.
1 change: 1 addition & 0 deletions dist/localCookie.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion dist/storage.js

This file was deleted.

1 change: 1 addition & 0 deletions module/localCookie.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion module/storage.js

This file was deleted.

4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ yarn add github:WebPlatformForEmbedded/localCookies
Next you can `import` the localCookies dependency into your own script and start implementing it from there.

```js
import Storage from './module/Storage.js',
import Storage from './module/localCookie.js',
// or
const Storage = require('./module/Storage.js')
const Storage = require('./module/localCookie.js')
```

## Build
Expand Down
8 changes: 4 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import peerDepsExternal from 'rollup-plugin-peer-deps-external'

export default [
{
input: './src/storage.js',
input: './src/localCookie.js',
output: {
file: './dist/storage.js',
file: './dist/localCookie.js',
format: 'iife',
name: 'Storage',
},
plugins: [resolve({ browser: true }), commonjs(), babel(), uglify()],
},
{
input: './src/storage.js',
input: './src/localCookie.js',
output: {
file: './module/storage.js',
file: './module/localCookie.js',
format: 'esm',
name: 'Storage',
},
Expand Down
11 changes: 7 additions & 4 deletions src/storage.js → src/localCookie.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// LocalStorage and Cookie storage library
class Storage {
constructor() {
if (this._checkIfLocalStorageWorks() === true)
class localCookie {
constructor(options) {
options = options || {}
this.forceCookies = options.forceCookies || false

if (this._checkIfLocalStorageWorks() === true && options.forceCookies !== true)
return {
getItem : this._getItemLocalStorage,
setItem : this._setItemLocalStorage,
Expand Down Expand Up @@ -96,4 +99,4 @@ class Storage {
}
}

export default Storage
export default localCookie
73 changes: 64 additions & 9 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Storage from '../src/storage.js'
let ls = new Storage()
import Storage from '../src/localCookie.js'

let expect = chai.expect

describe('Storage', () => {
describe('local Storage', () => {
let ls = new Storage()
it('should return an object', () => {
expect(ls).to.be.a('object')
})
Expand All @@ -18,9 +19,7 @@ describe('Storage', () => {
it('should have a clear function', () => {
expect(ls.clear).to.be.a('function')
})
})

describe('Store and get data', () => {
it('should accept a key/value "a" with value "b"', () => {
let _r = ls.setItem('a','b')
expect( _r ).to.be.a('undefined')
Expand All @@ -34,9 +33,7 @@ describe('Store and get data', () => {
ls.setItem('anumber', 1)
expect( ls.getItem('anumber') ).to.be.a('string')
})
})

describe('Remove data', () => {
it('should removeItem key "a"', () => {
let _r = ls.removeItem('a')
expect( _r ).to.be.a('undefined')
Expand All @@ -46,9 +43,7 @@ describe('Remove data', () => {
let _r = ls.getItem('a')
expect( _r ).to.be.a('null')
})
})

describe('Clear data', () => {
it('set key "a" and key "b"', () => {
ls.setItem('a', 'a')
ls.setItem('b', 'b')
Expand All @@ -67,4 +62,64 @@ describe('Clear data', () => {
})
})

describe('Cookies ', () => {
let cs = new Storage({ forceCookies : true })
it('should return an object', () => {
expect(cs).to.be.a('object')
})

it('should have a setItem function', () => {
expect(cs.setItem).to.be.a('function')
})

it('should have a removeItem function', () => {
expect(cs.removeItem).to.be.a('function')
})

it('should have a clear function', () => {
expect(cs.clear).to.be.a('function')
})

it('should accept a key/value "a" with value "b"', () => {
let _r = cs.setItem('a','b')
expect( _r ).to.be.a('undefined')
})
it('should return the same value "b" for key "a" ', () => {
let _r = cs.getItem('a')
expect( _r ).to.be.equal('b')
})

it('should store a number as a string', () => {
cs.setItem('anumber', 1)
expect( cs.getItem('anumber') ).to.be.a('string')
})

it('should removeItem key "a"', () => {
let _r = cs.removeItem('a')
expect( _r ).to.be.a('undefined')
})

it('should return a null value for key "a"', () => {
let _r = cs.getItem('a')
expect( _r ).to.be.a('null')
})

it('set key "a" and key "b"', () => {
cs.setItem('a', 'a')
cs.setItem('b', 'b')
})

it('call clear()', () => {
expect( cs.clear() ).to.be.a('undefined')
})

it('should not have a value for key "a"', () => {
expect( cs.getItem('a') ).to.be.a('null')
})

it('should not have a value for key "b"', () => {
expect( cs.getItem('b') ).to.be.a('null')
})
})

mocha.run()

0 comments on commit e3fbd02

Please sign in to comment.