✅ test: 覆盖弹窗生命周期、存储合并与插件页的时长选项
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
// The browser-local trigger switches. Storage is foreign input: it can be
|
||||
// absent, hold another version's shape, or refuse to write, and none of those
|
||||
// may stop the plugin from working with its defaults.
|
||||
// The browser-local preferences: the three trigger switches and the popup
|
||||
// lifetime. Storage is foreign input: it can be absent, hold another version's
|
||||
// shape, or refuse to write, and none of that may stop the plugin — nor may
|
||||
// writing one preference ever clear another.
|
||||
import { afterEach, test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
|
||||
import { STORAGE_KEY, readStoredKinds, writeStoredKinds } from '../../src/client/core/storage.js'
|
||||
import {
|
||||
STORAGE_KEY,
|
||||
readStoredDismissMs,
|
||||
readStoredKinds,
|
||||
writeStoredDismissMs,
|
||||
writeStoredKinds,
|
||||
} from '../../src/client/core/storage.js'
|
||||
import { TOAST_DURATION_MS } from '../../src/client/constants.js'
|
||||
|
||||
/** Install a storage double, optionally one that throws. */
|
||||
function useStorage(behaviour) {
|
||||
@@ -26,20 +34,22 @@ afterEach(() => {
|
||||
delete globalThis.localStorage
|
||||
})
|
||||
|
||||
test('everything is on when this browser has no stored choice', () => {
|
||||
test('everything is on, with the shipped popup lifetime, when this browser has no stored choice', () => {
|
||||
useStorage()
|
||||
assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true })
|
||||
assert.equal(readStoredDismissMs(), TOAST_DURATION_MS)
|
||||
})
|
||||
|
||||
test('without storage at all the defaults still apply', () => {
|
||||
assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true })
|
||||
assert.equal(readStoredDismissMs(), TOAST_DURATION_MS)
|
||||
assert.doesNotThrow(() => writeStoredKinds({ completion: false, approval: true, question: true }))
|
||||
assert.doesNotThrow(() => writeStoredDismissMs(0))
|
||||
})
|
||||
|
||||
test('a stored switch survives a round trip, and unknown or broken values do not', () => {
|
||||
const data = useStorage()
|
||||
writeStoredKinds({ completion: false, approval: true, question: false })
|
||||
assert.equal(data.get(STORAGE_KEY), JSON.stringify({ completion: false, approval: true, question: false }))
|
||||
assert.deepEqual(readStoredKinds(), { completion: false, approval: true, question: false })
|
||||
|
||||
data.set(STORAGE_KEY, '{ not json')
|
||||
@@ -52,10 +62,41 @@ test('a stored switch survives a round trip, and unknown or broken values do not
|
||||
assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true })
|
||||
})
|
||||
|
||||
test('the popup lifetime survives a round trip, including "never"', () => {
|
||||
const data = useStorage()
|
||||
writeStoredDismissMs(30000)
|
||||
assert.equal(readStoredDismissMs(), 30000)
|
||||
|
||||
writeStoredDismissMs(0)
|
||||
assert.equal(readStoredDismissMs(), 0, 'zero means the card waits for the user')
|
||||
|
||||
// A value this version does not offer is ignored rather than trusted.
|
||||
data.set(STORAGE_KEY, JSON.stringify({ dismissAfterMs: 1234 }))
|
||||
assert.equal(readStoredDismissMs(), TOAST_DURATION_MS)
|
||||
data.set(STORAGE_KEY, JSON.stringify({ dismissAfterMs: -1 }))
|
||||
assert.equal(readStoredDismissMs(), TOAST_DURATION_MS)
|
||||
data.set(STORAGE_KEY, JSON.stringify({ dismissAfterMs: '6000' }))
|
||||
assert.equal(readStoredDismissMs(), TOAST_DURATION_MS)
|
||||
})
|
||||
|
||||
test('writing one preference keeps every other one', () => {
|
||||
useStorage()
|
||||
writeStoredKinds({ completion: false, approval: false, question: false })
|
||||
writeStoredDismissMs(0)
|
||||
assert.deepEqual(readStoredKinds(), { completion: false, approval: false, question: false })
|
||||
assert.equal(readStoredDismissMs(), 0)
|
||||
|
||||
// …and the other way round: the switches do not reset the lifetime.
|
||||
writeStoredKinds({ completion: true, approval: true, question: true })
|
||||
assert.equal(readStoredDismissMs(), 0)
|
||||
})
|
||||
|
||||
test('storage that refuses to be read or written never throws', () => {
|
||||
useStorage('read-throws')
|
||||
assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true })
|
||||
assert.equal(readStoredDismissMs(), TOAST_DURATION_MS)
|
||||
|
||||
useStorage('write-throws')
|
||||
assert.doesNotThrow(() => writeStoredKinds({ completion: false, approval: false, question: false }))
|
||||
assert.doesNotThrow(() => writeStoredDismissMs(3000))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user