|
3 | 3 | * SPDX-License-Identifier: GPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { expect, test, vi } from 'vitest' |
| 6 | +import { beforeEach, expect, test, vi } from 'vitest' |
7 | 7 | import { loadState } from '../lib/index.ts' |
8 | 8 |
|
9 | | -/** |
10 | | - * Mock initial state input elements |
11 | | - * @param app first part of the key |
12 | | - * @param key second part of the key |
13 | | - * @param value value to be stored |
14 | | - */ |
15 | | -function appendInput(app: string, key: string, value: string) { |
16 | | - const input = document.createElement('input') |
17 | | - input.setAttribute('type', 'hidden') |
18 | | - input.setAttribute('id', `initial-state-${app}-${key}`) |
19 | | - input.setAttribute('value', btoa(JSON.stringify(value))) |
20 | | - document.querySelector('body')!.appendChild(input) |
21 | | -} |
| 9 | +beforeEach(() => vi.resetAllMocks()) |
22 | 10 |
|
23 | 11 | test('throw if nothing found', () => { |
24 | 12 | expect(() => loadState('test', 'key')).toThrow(new Error('Could not find initial state key of test')) |
@@ -72,5 +60,19 @@ test('returns fallback if state cannot be parsed but fallback is provided', () = |
72 | 60 | }) |
73 | 61 |
|
74 | 62 | expect(loadState('app', 'key', 'fallback')).toBe('fallback') |
75 | | - expect(errorLog).toHaveBeenCalledOnce() |
| 63 | + expect(errorLog).toHaveBeenCalled() |
76 | 64 | }) |
| 65 | + |
| 66 | +/** |
| 67 | + * Mock initial state input elements |
| 68 | + * @param app first part of the key |
| 69 | + * @param key second part of the key |
| 70 | + * @param value value to be stored |
| 71 | + */ |
| 72 | +function appendInput(app: string, key: string, value: string) { |
| 73 | + const input = document.createElement('input') |
| 74 | + input.setAttribute('type', 'hidden') |
| 75 | + input.setAttribute('id', `initial-state-${app}-${key}`) |
| 76 | + input.setAttribute('value', btoa(JSON.stringify(value))) |
| 77 | + document.querySelector('body')!.appendChild(input) |
| 78 | +} |
0 commit comments