aboutsummaryrefslogtreecommitdiff
path: root/test/unit
diff options
context:
space:
mode:
authorHenry Jameson <me@hjkos.com>2022-08-10 02:19:07 +0300
committerHenry Jameson <me@hjkos.com>2022-08-10 02:19:07 +0300
commitaa41cedd932e88b030ecc3cc54848b5aa300eec3 (patch)
tree7ff210e74e4751ac2c2ea65e2bf8ad8f8eecaa44 /test/unit
parent750696643ffde27c1e8ae5a50253bbee5bfc4249 (diff)
initial prefs storage work
Diffstat (limited to 'test/unit')
-rw-r--r--test/unit/specs/modules/serverSideStorage.spec.js94
1 files changed, 94 insertions, 0 deletions
diff --git a/test/unit/specs/modules/serverSideStorage.spec.js b/test/unit/specs/modules/serverSideStorage.spec.js
index e06c6ada..6059dc84 100644
--- a/test/unit/specs/modules/serverSideStorage.spec.js
+++ b/test/unit/specs/modules/serverSideStorage.spec.js
@@ -7,6 +7,7 @@ import {
_getRecentData,
_getAllFlags,
_mergeFlags,
+ _mergePrefs,
_resetFlags,
mutations,
defaultState,
@@ -28,6 +29,7 @@ describe('The serverSideStorage module', () => {
expect(state.cache._version).to.eql(VERSION)
expect(state.cache._timestamp).to.be.a('number')
expect(state.cache.flagStorage).to.eql(defaultState.flagStorage)
+ expect(state.cache.prefsStorage).to.eql(defaultState.prefsStorage)
})
it('should initialize storage with proper flags for new users if none present', () => {
@@ -36,6 +38,7 @@ describe('The serverSideStorage module', () => {
expect(state.cache._version).to.eql(VERSION)
expect(state.cache._timestamp).to.be.a('number')
expect(state.cache.flagStorage).to.eql(newUserFlags)
+ expect(state.cache.prefsStorage).to.eql(defaultState.prefsStorage)
})
it('should merge flags even if remote timestamp is older', () => {
@@ -57,6 +60,9 @@ describe('The serverSideStorage module', () => {
flagStorage: {
...defaultState.flagStorage,
updateCounter: 1
+ },
+ prefsStorage: {
+ ...defaultState.flagStorage,
}
}
}
@@ -157,6 +163,94 @@ describe('The serverSideStorage module', () => {
})
})
+ describe('_mergePrefs', () => {
+ it('should prefer recent and apply journal to it', () => {
+ expect(
+ _mergePrefs(
+ // RECENT
+ {
+ simple: { a: 1, b: 0, c: true },
+ _journal: [
+ { path: 'simple.b', operation: 'set', args: [0], timestamp: 2 },
+ { path: 'simple.c', operation: 'set', args: [true], timestamp: 4 }
+ ]
+ },
+ // STALE
+ {
+ simple: { a: 1, b: 1, c: false },
+ _journal: [
+ { path: 'simple.a', operation: 'set', args: [1], timestamp: 1 },
+ { path: 'simple.b', operation: 'set', args: [1], timestamp: 3 }
+ ]
+ }
+ )
+ ).to.eql({
+ simple: { a: 1, b: 1, c: true },
+ _journal: [
+ { path: 'simple.a', operation: 'set', args: [1], timestamp: 1 },
+ { path: 'simple.b', operation: 'set', args: [1], timestamp: 3 },
+ { path: 'simple.c', operation: 'set', args: [true], timestamp: 4 }
+ ]
+ })
+ })
+
+ it('should allow setting falsy values', () => {
+ expect(
+ _mergePrefs(
+ // RECENT
+ {
+ simple: { a: 1, b: 0, c: false },
+ _journal: [
+ { path: 'simple.b', operation: 'set', args: [0], timestamp: 2 },
+ { path: 'simple.c', operation: 'set', args: [false], timestamp: 4 }
+ ]
+ },
+ // STALE
+ {
+ simple: { a: 0, b: 0, c: true },
+ _journal: [
+ { path: 'simple.a', operation: 'set', args: [0], timestamp: 1 },
+ { path: 'simple.b', operation: 'set', args: [0], timestamp: 3 }
+ ]
+ }
+ )
+ ).to.eql({
+ simple: { a: 0, b: 0, c: false },
+ _journal: [
+ { path: 'simple.a', operation: 'set', args: [0], timestamp: 1 },
+ { path: 'simple.b', operation: 'set', args: [0], timestamp: 3 },
+ { path: 'simple.c', operation: 'set', args: [false], timestamp: 4 }
+ ]
+ })
+ })
+
+ it('should work with strings', () => {
+ expect(
+ _mergePrefs(
+ // RECENT
+ {
+ simple: { a: 'foo' },
+ _journal: [
+ { path: 'simple.a', operation: 'set', args: ['foo'], timestamp: 2 }
+ ]
+ },
+ // STALE
+ {
+ simple: { a: 'bar' },
+ _journal: [
+ { path: 'simple.a', operation: 'set', args: ['bar'], timestamp: 4 }
+ ]
+ }
+ )
+ ).to.eql({
+ simple: { a: 'bar' },
+ _journal: [
+ { path: 'simple.a', operation: 'set', args: ['bar'], timestamp: 4 }
+ ]
+ })
+ })
+ })
+
describe('_resetFlags', () => {
it('should reset all known flags to 0 when reset flag is set to > 0 and < 9000', () => {
const totalFlags = { a: 0, b: 3, reset: 1 }