2024-02-04 12:14:42 +01:00
|
|
|
import { expect } from 'chai'
|
2022-09-09 20:20:38 +02:00
|
|
|
import { Design, Store } from '../src/index.mjs'
|
2022-08-25 11:47:54 +02:00
|
|
|
|
2022-09-07 16:16:33 +02:00
|
|
|
const store = new Store()
|
2018-08-12 13:32:53 +02:00
|
|
|
|
2022-08-27 09:27:07 +02:00
|
|
|
describe('Store', () => {
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should set/get a top-level store value', () => {
|
|
|
|
store.set('foo', 'bar')
|
|
|
|
expect(store.get('foo')).to.equal('bar')
|
|
|
|
})
|
2018-08-12 13:32:53 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should set/get a nested store value', () => {
|
|
|
|
store.set('some.nested.key.foo', 'bar')
|
|
|
|
expect(store.get('some.nested.key').foo).to.equal('bar')
|
|
|
|
})
|
2018-08-12 13:32:53 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should set a store value only if unset', () => {
|
|
|
|
store.setIfUnset('dew.few', 'baz')
|
|
|
|
store.setIfUnset('dew.few', 'schmoo')
|
|
|
|
expect(store.get('dew').few).to.equal('baz')
|
|
|
|
})
|
2022-09-07 16:16:33 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should push to an array value in the store', () => {
|
|
|
|
store.set('dew.few', ['baz', 'bar'])
|
2022-09-07 16:16:33 +02:00
|
|
|
// store.push is variadic
|
2022-09-09 20:20:38 +02:00
|
|
|
store.push('dew.few', 'boz', 'bor')
|
|
|
|
expect(store.get('dew').few.length).to.equal(4)
|
|
|
|
})
|
2022-09-07 16:16:33 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should emit a warning when retrieving a invalid key', () => {
|
2022-09-07 16:55:44 +02:00
|
|
|
const store = new Store()
|
2022-08-27 09:27:07 +02:00
|
|
|
store.get('nope')
|
2023-09-05 12:00:05 +02:00
|
|
|
expect(store.get('logs.warn').length).to.equal(1)
|
|
|
|
expect(store.get('logs.warn')[0]).to.equal('Store.get(key) on key `nope`, which is undefined')
|
2022-09-09 20:20:38 +02:00
|
|
|
})
|
2022-09-07 16:16:33 +02:00
|
|
|
|
2022-09-09 20:20:38 +02:00
|
|
|
it('Should add methods to the store from a plugin', () => {
|
2022-09-07 16:16:33 +02:00
|
|
|
const plugin = {
|
|
|
|
name: 'test',
|
|
|
|
version: 1,
|
|
|
|
store: [
|
2022-09-09 20:20:38 +02:00
|
|
|
[
|
2023-09-05 12:00:05 +02:00
|
|
|
'test.example.warn',
|
2022-09-09 20:20:38 +02:00
|
|
|
function (store, msg) {
|
2023-09-05 12:00:05 +02:00
|
|
|
store.set('test.message.warn', msg)
|
2022-09-09 20:20:38 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'test.example.info',
|
|
|
|
function (store, msg) {
|
|
|
|
store.set('test.message.info', msg)
|
|
|
|
},
|
|
|
|
],
|
|
|
|
],
|
2022-09-07 16:16:33 +02:00
|
|
|
}
|
|
|
|
const part = {
|
|
|
|
name: 'example.part',
|
2022-10-13 16:19:36 +02:00
|
|
|
plugins: [plugin],
|
2022-09-10 19:13:43 +02:00
|
|
|
draft: ({ store, part }) => {
|
2023-09-05 12:00:05 +02:00
|
|
|
store.test.example.warn('hello warn')
|
2022-09-07 16:16:33 +02:00
|
|
|
store.test.example.info('hello info')
|
2022-09-14 16:56:42 +02:00
|
|
|
|
|
|
|
return part
|
2022-09-09 20:20:38 +02:00
|
|
|
},
|
2022-09-07 16:16:33 +02:00
|
|
|
}
|
2022-09-24 12:44:41 +02:00
|
|
|
const Test = new Design({ parts: [part] })
|
2022-09-07 16:16:33 +02:00
|
|
|
const pattern = new Test()
|
|
|
|
pattern.draft()
|
2023-09-05 12:00:05 +02:00
|
|
|
expect(pattern.setStores[0].get('test.message.warn')).to.equal('hello warn')
|
2022-09-24 12:44:41 +02:00
|
|
|
expect(pattern.setStores[0].get('test.message.info')).to.equal('hello info')
|
2022-09-09 20:20:38 +02:00
|
|
|
})
|
2022-09-07 19:52:50 +02:00
|
|
|
|
2022-09-19 23:35:52 +02:00
|
|
|
it('Should log a warning when trying to extend a protected method via the constructor', () => {
|
|
|
|
const store = new Store([['get', () => false]])
|
2023-09-05 12:00:05 +02:00
|
|
|
expect(store.logs.warn.length).to.equal(1)
|
|
|
|
expect(store.logs.warn[0]).to.equal('You cannot overwrite `store.get()`')
|
2022-09-19 23:35:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should log a warning when trying to extend a protected method via the extend', () => {
|
|
|
|
const store = new Store()
|
|
|
|
store.extend([['get', () => false]])
|
2023-09-05 12:00:05 +02:00
|
|
|
expect(store.logs.warn.length).to.equal(1)
|
|
|
|
expect(store.logs.warn[0]).to.equal('You cannot overwrite `store.get()`')
|
2022-09-19 23:35:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should extend the store with a new method via the constructor', () => {
|
|
|
|
const store = new Store([['test', () => true]])
|
|
|
|
expect(store.test()).to.equal(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('Should log a warning when pushing to a non-array key', () => {
|
|
|
|
const store = new Store()
|
2022-10-13 16:19:36 +02:00
|
|
|
store.push('test', 1)
|
2023-09-05 12:00:05 +02:00
|
|
|
expect(store.logs.warn.length).to.equal(1)
|
|
|
|
expect(store.logs.warn[0]).to.equal(
|
2022-10-13 16:19:36 +02:00
|
|
|
'Store.push(value) on key `test`, but key does not hold an array'
|
|
|
|
)
|
2022-09-19 23:35:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should log a warning when setting an undefined value with set()', () => {
|
|
|
|
const store = new Store()
|
|
|
|
store.set('test')
|
2023-09-05 12:00:05 +02:00
|
|
|
expect(store.logs.warn.length).to.equal(1)
|
|
|
|
expect(store.logs.warn[0]).to.equal('Store.set(value) on key `test`, but value is undefined')
|
2022-09-19 23:35:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should log a warning when setting an undefined value with setIfUnset()', () => {
|
|
|
|
const store = new Store()
|
|
|
|
store.setIfUnset('test')
|
2023-09-05 12:00:05 +02:00
|
|
|
expect(store.logs.warn.length).to.equal(1)
|
|
|
|
expect(store.logs.warn[0]).to.equal(
|
2022-10-13 16:19:36 +02:00
|
|
|
'Store.setIfUnset(value) on key `test`, but value is undefined'
|
|
|
|
)
|
2022-09-19 23:35:52 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
it('Should unset a value', () => {
|
|
|
|
const store = new Store()
|
|
|
|
store.set('test', 1980)
|
|
|
|
expect(store.get('test')).to.equal(1980)
|
|
|
|
store.unset('test')
|
|
|
|
expect(typeof store.get('test')).to.equal('undefined')
|
|
|
|
})
|
2022-09-09 20:20:38 +02:00
|
|
|
})
|