|
9 | 9 | } from '../../protocol'
|
10 | 10 | import { Configuration, Observable } from '../api'
|
11 | 11 | import { observableValue } from '../util'
|
12 |
| -import { createExtConfiguration } from './configuration' |
| 12 | +import { createExtConfiguration, setValueAtKeyPath } from './configuration' |
13 | 13 |
|
14 | 14 | interface Settings {
|
15 | 15 | [key: string]: string
|
@@ -41,16 +41,81 @@ describe('ExtConfiguration', () => {
|
41 | 41 | })
|
42 | 42 |
|
43 | 43 | describe('update', () => {
|
44 |
| - it('sends to the client', async () => { |
| 44 | + it('sends to the client and immediately reflects locally', async () => { |
45 | 45 | const { extConfiguration, mockConnection } = create()
|
46 | 46 | mockConnection.mockResults.set(ConfigurationUpdateRequest.type.method, void 0)
|
47 |
| - await extConfiguration.update('a', 'b') |
| 47 | + const updated = extConfiguration.update('a', 'b') |
| 48 | + const want = { a: 'b' } as Settings |
| 49 | + assert.deepStrictEqual(observableValue(extConfiguration), want) |
| 50 | + await updated |
| 51 | + assert.deepStrictEqual(observableValue(extConfiguration), want) |
48 | 52 | assert.deepStrictEqual(mockConnection.sentMessages, [
|
49 | 53 | {
|
50 | 54 | method: ConfigurationUpdateRequest.type.method,
|
51 | 55 | params: { path: ['a'], value: 'b' } as ConfigurationUpdateParams,
|
52 | 56 | },
|
53 | 57 | ])
|
54 | 58 | })
|
| 59 | + |
| 60 | + it('handles interleaved update calls and didChangeConfiguration notifications', async () => { |
| 61 | + const { extConfiguration, mockConnection } = create() |
| 62 | + mockConnection.mockResults.set(ConfigurationUpdateRequest.type.method, void 0) |
| 63 | + const updated = extConfiguration.update('a', 'b') |
| 64 | + assert.deepStrictEqual(observableValue(extConfiguration), { a: 'b' } as Settings) |
| 65 | + mockConnection.recvNotification(DidChangeConfigurationNotification.type.method, { |
| 66 | + configurationCascade: { merged: { c: 'd' } as Settings }, |
| 67 | + } as DidChangeConfigurationParams) |
| 68 | + assert.deepStrictEqual(observableValue(extConfiguration), { c: 'd' } as Settings) |
| 69 | + await updated |
| 70 | + assert.deepStrictEqual(observableValue(extConfiguration), { c: 'd' } as Settings) |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('setValueAtKeyPath', () => { |
| 76 | + it('overwrites the top level', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, [], { b: 2 }), { b: 2 })) |
| 77 | + it('overwrites an existing property', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, ['a'], 2), { a: 2 })) |
| 78 | + it('sets a new property', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, ['b'], 2), { a: 1, b: 2 })) |
| 79 | + it('sets a property overwriting an array', () => assert.deepStrictEqual(setValueAtKeyPath([1], ['a'], 2), { a: 2 })) |
| 80 | + it('sets a property overwriting a primitive', () => |
| 81 | + assert.deepStrictEqual(setValueAtKeyPath(1 as any, ['a'], 2), { a: 2 })) |
| 82 | + it('overwrites an existing nested property', () => |
| 83 | + assert.deepStrictEqual(setValueAtKeyPath({ a: { b: 1 } }, ['a', 'b'], 2), { a: { b: 2 } })) |
| 84 | + it('deletes a property', () => |
| 85 | + assert.deepStrictEqual(setValueAtKeyPath({ a: 1, b: 2 }, ['a'], undefined), { b: 2 })) |
| 86 | + it('sets a new nested property', () => |
| 87 | + assert.deepStrictEqual(setValueAtKeyPath({ a: { b: 1 } }, ['a', 'c'], 2), { a: { b: 1, c: 2 } })) |
| 88 | + it('sets a new deeply nested property', () => |
| 89 | + assert.deepStrictEqual(setValueAtKeyPath({ a: { b: { c: 1 } } }, ['a', 'b', 'd'], 2), { |
| 90 | + a: { b: { c: 1, d: 2 } }, |
| 91 | + })) |
| 92 | + it('overwrites an object', () => assert.deepStrictEqual(setValueAtKeyPath({ a: { b: 1 } }, ['a'], 2), { a: 2 })) |
| 93 | + it('sets a value that requires a new object', () => |
| 94 | + assert.deepStrictEqual(setValueAtKeyPath({}, ['a', 'b'], 1), { a: { b: 1 } })) |
| 95 | + |
| 96 | + it('overwrites an existing index', () => assert.deepStrictEqual(setValueAtKeyPath([1], [0], 2), [2])) |
| 97 | + it('inserts a new index', () => assert.deepStrictEqual(setValueAtKeyPath([1], [1], 2), [1, 2])) |
| 98 | + it('inserts a new index at end', () => assert.deepStrictEqual(setValueAtKeyPath([1, 2], [-1], 3), [1, 2, 3])) |
| 99 | + it('inserts an index overwriting an object', () => assert.deepStrictEqual(setValueAtKeyPath({ a: 1 }, [0], 2), [2])) |
| 100 | + it('inserts an index overwriting a primitive', () => |
| 101 | + assert.deepStrictEqual(setValueAtKeyPath(1 as any, [0], 2), [2])) |
| 102 | + it('overwrites an existing nested index', () => |
| 103 | + assert.deepStrictEqual(setValueAtKeyPath([1, [2]], [1, 0], 3), [1, [3]])) |
| 104 | + it('deletes an index', () => assert.deepStrictEqual(setValueAtKeyPath([1, 2, 3], [1], undefined), [1, 3])) |
| 105 | + it('sets a new nested index', () => |
| 106 | + assert.deepStrictEqual(setValueAtKeyPath([1, [1, 2, [1, 2, 3, 4]]], [1, 2, 3], 5), [1, [1, 2, [1, 2, 3, 5]]])) |
| 107 | + it('inserts a new nested index at end', () => |
| 108 | + assert.deepStrictEqual(setValueAtKeyPath([1, [2]], [1, -1], 3), [1, [2, 3]])) |
| 109 | + it('overwrites an array', () => assert.deepStrictEqual(setValueAtKeyPath([1, [2]], [1], 3), [1, 3])) |
| 110 | + it('sets a value that requires a new array', () => assert.deepStrictEqual(setValueAtKeyPath([], [0, 0], 1), [[1]])) |
| 111 | + |
| 112 | + it('sets a nested property (and does not modify input)', () => { |
| 113 | + const input = { a: [{}, { b: [1, 2] }] } |
| 114 | + const origInput = JSON.parse(JSON.stringify(input)) |
| 115 | + assert.deepStrictEqual(setValueAtKeyPath(input, ['a', 1, 'b', -1], { c: 3 }), { |
| 116 | + a: [{}, { b: [1, 2, { c: 3 }] }], |
| 117 | + }) |
| 118 | + assert.deepStrictEqual(input, origInput) |
55 | 119 | })
|
| 120 | + it('throws on invalid key type', () => assert.throws(() => setValueAtKeyPath({}, [true as any], {}))) |
56 | 121 | })
|
0 commit comments