summaryrefslogtreecommitdiff
path: root/web/test/tab-persistence.test.mjs
blob: 9311453e8440b1641a2beb5ea75a2817c7e68de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// tab-persistence.test.mjs — TDD tests for main-tab localStorage persistence
//
// Run with: node --test web/test/tab-persistence.test.mjs

import { describe, it, beforeEach } from 'node:test';
import assert from 'node:assert/strict';

// ── localStorage mock ──────────────────────────────────────────────────────────
// Must be set up before importing app.js so the module sees the global.
const store = new Map();
globalThis.localStorage = {
  getItem:    (k)    => store.has(k) ? store.get(k) : null,
  setItem:    (k, v) => store.set(k, String(v)),
  removeItem: (k)    => store.delete(k),
  clear:      ()     => store.clear(),
};

import { getActiveMainTab, setActiveMainTab } from '../app.js';

describe('getActiveMainTab', () => {
  beforeEach(() => store.clear());

  it('returns "queue" when localStorage has no stored value', () => {
    assert.equal(getActiveMainTab(), 'queue');
  });

  it('returns the tab name stored by setActiveMainTab', () => {
    setActiveMainTab('settings');
    assert.equal(getActiveMainTab(), 'settings');
  });

  it('returns "queue" after localStorage value is removed', () => {
    setActiveMainTab('stats');
    localStorage.removeItem('activeMainTab');
    assert.equal(getActiveMainTab(), 'queue');
  });

  it('reflects the most recent setActiveMainTab call', () => {
    setActiveMainTab('stats');
    setActiveMainTab('running');
    assert.equal(getActiveMainTab(), 'running');
  });
});

describe('setActiveMainTab', () => {
  beforeEach(() => store.clear());

  it('writes the tab name to localStorage under key "activeMainTab"', () => {
    setActiveMainTab('drops');
    assert.equal(localStorage.getItem('activeMainTab'), 'drops');
  });

  it('overwrites a previously stored tab', () => {
    setActiveMainTab('queue');
    setActiveMainTab('interrupted');
    assert.equal(localStorage.getItem('activeMainTab'), 'interrupted');
  });
});