|
| 1 | +import { describe, expect, test } from 'vitest'; |
| 2 | +import { createContext, MissingContextError } from './context.ts'; |
| 3 | +import { Node } from './node.ts'; |
| 4 | + |
| 5 | +const theme = createContext<string>('theme'); |
| 6 | +const count = createContext<number>('count'); |
| 7 | +const locale = createContext<string>('locale', 'en'); |
| 8 | + |
| 9 | +describe('context', () => { |
| 10 | + describe('on a single node', () => { |
| 11 | + test('get returns undefined for a context that was never set', () => { |
| 12 | + expect(theme.get(new Node())).toBeUndefined(); |
| 13 | + }); |
| 14 | + |
| 15 | + test('does not inherit values from Object.prototype', () => { |
| 16 | + const constructor = createContext('constructor'); |
| 17 | + |
| 18 | + expect(constructor.get(new Node())).toBeUndefined(); |
| 19 | + }); |
| 20 | + |
| 21 | + test('get returns the value after it is set', () => { |
| 22 | + const n = new Node(); |
| 23 | + theme.set(n, 'dark'); |
| 24 | + expect(theme.get(n)).toBe('dark'); |
| 25 | + }); |
| 26 | + |
| 27 | + test('the last set wins', () => { |
| 28 | + const n = new Node(); |
| 29 | + theme.set(n, 'dark'); |
| 30 | + theme.set(n, 'light'); |
| 31 | + expect(theme.get(n)).toBe('light'); |
| 32 | + }); |
| 33 | + |
| 34 | + test('keeps falsy values — 0 is stored, not treated as unset', () => { |
| 35 | + const n = new Node(); |
| 36 | + count.set(n, 0); |
| 37 | + expect(count.get(n)).toBe(0); |
| 38 | + }); |
| 39 | + |
| 40 | + test('distinct contexts do not collide', () => { |
| 41 | + const n = new Node(); |
| 42 | + theme.set(n, 'dark'); |
| 43 | + expect(count.get(n)).toBeUndefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + test('expect returns the value when the context is set', () => { |
| 47 | + const n = new Node(); |
| 48 | + theme.set(n, 'dark'); |
| 49 | + expect(theme.expect(n)).toBe('dark'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('expect throws MissingContextError when unset with no default', () => { |
| 53 | + expect(() => theme.expect(new Node())).toThrow(MissingContextError); |
| 54 | + }); |
| 55 | + |
| 56 | + test('expect falls back to the default when unset', () => { |
| 57 | + expect(locale.expect(new Node())).toBe('en'); |
| 58 | + }); |
| 59 | + |
| 60 | + test('a set value takes precedence over the default', () => { |
| 61 | + const n = new Node(); |
| 62 | + locale.set(n, 'fr'); |
| 63 | + expect(locale.expect(n)).toBe('fr'); |
| 64 | + }); |
| 65 | + |
| 66 | + test('get returns the default when the context is unset', () => { |
| 67 | + expect(locale.get(new Node())).toBe('en'); |
| 68 | + }); |
| 69 | + |
| 70 | + test('a set value overrides the default for get', () => { |
| 71 | + const n = new Node(); |
| 72 | + locale.set(n, 'fr'); |
| 73 | + expect(locale.get(n)).toBe('fr'); |
| 74 | + }); |
| 75 | + |
| 76 | + test('hasOwn is false before a value is set', () => { |
| 77 | + expect(theme.hasOwn(new Node())).toBe(false); |
| 78 | + }); |
| 79 | + |
| 80 | + test('hasOwn is true once a value is set on the node', () => { |
| 81 | + const n = new Node(); |
| 82 | + theme.set(n, 'dark'); |
| 83 | + expect(theme.hasOwn(n)).toBe(true); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('inheritance through the node tree', () => { |
| 88 | + test('a child inherits a value set on its parent', () => { |
| 89 | + const parent = new Node(); |
| 90 | + theme.set(parent, 'dark'); |
| 91 | + expect(theme.get(new Node(parent))).toBe('dark'); |
| 92 | + }); |
| 93 | + |
| 94 | + test('a value set on a child shadows the parent for that child only', () => { |
| 95 | + const parent = new Node(); |
| 96 | + theme.set(parent, 'dark'); |
| 97 | + const child = new Node(parent); |
| 98 | + theme.set(child, 'light'); |
| 99 | + expect(theme.get(child)).toBe('light'); |
| 100 | + expect(theme.get(parent)).toBe('dark'); |
| 101 | + }); |
| 102 | + |
| 103 | + test('setting a value on a child does not leak up to the parent', () => { |
| 104 | + const parent = new Node(); |
| 105 | + const child = new Node(parent); |
| 106 | + theme.set(child, 'light'); |
| 107 | + expect(theme.get(parent)).toBeUndefined(); |
| 108 | + }); |
| 109 | + |
| 110 | + test('expect inherits an ancestor value', () => { |
| 111 | + const parent = new Node(); |
| 112 | + theme.set(parent, 'dark'); |
| 113 | + expect(theme.expect(new Node(parent))).toBe('dark'); |
| 114 | + }); |
| 115 | + |
| 116 | + test('an inherited value resolves through multiple levels', () => { |
| 117 | + const grand = new Node(); |
| 118 | + theme.set(grand, 'dark'); |
| 119 | + const parent = new Node(grand); |
| 120 | + expect(theme.get(new Node(parent))).toBe('dark'); |
| 121 | + }); |
| 122 | + |
| 123 | + test('hasOwn is false for a value that is only inherited', () => { |
| 124 | + const parent = new Node(); |
| 125 | + theme.set(parent, 'dark'); |
| 126 | + expect(theme.hasOwn(new Node(parent))).toBe(false); |
| 127 | + }); |
| 128 | + |
| 129 | + test('hasOwn is true only for the node that owns the value', () => { |
| 130 | + const parent = new Node(); |
| 131 | + theme.set(parent, 'dark'); |
| 132 | + const child = new Node(parent); |
| 133 | + theme.set(child, 'light'); |
| 134 | + expect(theme.hasOwn(child)).toBe(true); |
| 135 | + expect(theme.hasOwn(parent)).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + test('expect falls back to the default when neither node nor ancestors set it', () => { |
| 139 | + const parent = new Node(); |
| 140 | + expect(locale.expect(new Node(parent))).toBe('en'); |
| 141 | + }); |
| 142 | + |
| 143 | + test('get falls back to the default when neither node nor ancestors set it', () => { |
| 144 | + const parent = new Node(); |
| 145 | + expect(locale.get(new Node(parent))).toBe('en'); |
| 146 | + }); |
| 147 | + }); |
| 148 | +}); |
0 commit comments