generated from klientjs/open-stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow listen for changes made on parameters and bag objects
- Loading branch information
Showing
11 changed files
with
391 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import Klient from '..'; | ||
|
||
test('watch', () => { | ||
const unclonable = new Klient(); | ||
const { parameters } = new Klient({ | ||
request: { | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
}, | ||
arr: [{ prop: true }, unclonable] | ||
}); | ||
|
||
const innerParams = parameters as any; | ||
const clonedParams = parameters.all() as any; | ||
|
||
expect(JSON.stringify(Object.keys(clonedParams))).toBe( | ||
JSON.stringify(['url', 'extensions', 'request', 'debug', 'arr']) | ||
); | ||
expect(clonedParams.arr === innerParams).toBeFalsy(); | ||
expect(clonedParams.arr[0] === innerParams.arr[0]).toBeFalsy(); | ||
expect(clonedParams.arr[1] === innerParams.arr[1]).toBeTruthy(); | ||
|
||
const urlWatchSpy = jest.fn(); | ||
const requestWatchSpy = jest.fn(); | ||
|
||
parameters.watch('url', urlWatchSpy); | ||
// Check duplication | ||
parameters.watch('url', urlWatchSpy); | ||
parameters.watch('request.headers.Content-Type', requestWatchSpy); | ||
|
||
parameters.set('url', undefined); | ||
parameters.set('url', 'http://localhost'); | ||
|
||
expect(urlWatchSpy).toBeCalledTimes(1); | ||
expect(urlWatchSpy).toBeCalledWith('http://localhost', undefined); | ||
|
||
parameters.set('request.headers', {}); | ||
|
||
expect(requestWatchSpy).toBeCalledWith(undefined, 'application/json'); | ||
|
||
parameters.merge({ | ||
request: { | ||
headers: { | ||
'Content-Type': 'application/xml' | ||
} | ||
} | ||
}); | ||
|
||
expect(requestWatchSpy).toBeCalledWith('application/xml', undefined); | ||
}); | ||
|
||
test('unwatch', () => { | ||
const { parameters } = new Klient(); | ||
|
||
const testWatchSpy = jest.fn(); | ||
|
||
parameters.unwatch('test', () => undefined); | ||
parameters.watch('test', testWatchSpy); | ||
parameters.unwatch('test', testWatchSpy); | ||
|
||
parameters.set('test', true); | ||
|
||
expect(testWatchSpy).not.toBeCalled(); | ||
}); | ||
|
||
test('watch:deep', () => { | ||
const { parameters } = new Klient({ | ||
example: { | ||
nested: { | ||
test: true | ||
} | ||
} | ||
}); | ||
|
||
const exampleDeepSpy = jest.fn(); | ||
const exampleSpy = jest.fn(); | ||
const exampleNestedSpy = jest.fn(); | ||
const exampleNestedDeepSpy = jest.fn(); | ||
const exampleNestedTestSpy = jest.fn(); | ||
|
||
parameters.watch('example', exampleSpy); | ||
parameters.watch('example', exampleDeepSpy, true); | ||
parameters.watch('example.nested', exampleNestedSpy); | ||
parameters.watch('example.nested', exampleNestedDeepSpy, true); | ||
parameters.watch('example.nested.test', exampleNestedTestSpy); | ||
|
||
expect(parameters.watchers['example'].length).toBe(2); | ||
expect(parameters.watchers['example.nested'].length).toBe(2); | ||
expect(parameters.watchers['example.nested.test'].length).toBe(1); | ||
|
||
parameters.set('example.nested.test', false); | ||
|
||
expect(exampleSpy).not.toBeCalled(); | ||
expect(exampleNestedSpy).not.toBeCalled(); | ||
|
||
expect(exampleDeepSpy).toBeCalledWith( | ||
{ | ||
nested: { | ||
test: false | ||
} | ||
}, | ||
{ | ||
nested: { | ||
test: true | ||
} | ||
} | ||
); | ||
|
||
expect(exampleNestedDeepSpy).toBeCalledWith({ test: false }, { test: true }); | ||
expect(exampleNestedTestSpy).toBeCalledWith(false, true); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import * as deepmerge from 'deepmerge'; | ||
import * as objectPath from 'object-path'; | ||
|
||
import { isPlainObject, isPlainArray, softClone } from '../../toolbox/object'; | ||
import { watch, unwatch, invokeWatchers, getWatchers, Watchable, WatchCallback } from './watch'; | ||
|
||
type BagItems = Record<string, unknown>; | ||
|
||
export default class Bag implements BagItems, Watchable { | ||
[x: string]: unknown; | ||
|
||
constructor(items: BagItems = {}) { | ||
Object.keys(items).forEach((key) => { | ||
this[key] = items[key]; | ||
}); | ||
} | ||
|
||
get watchers() { | ||
return getWatchers(this); | ||
} | ||
|
||
get(path: string) { | ||
return objectPath.get(this.all(), path); | ||
} | ||
|
||
all() { | ||
return softClone(this); | ||
} | ||
|
||
set(path: string, value: unknown): this { | ||
const prevState = this.all(); | ||
|
||
objectPath.set(this, path, value); | ||
|
||
return invokeWatchers(this, this.all(), prevState); | ||
} | ||
|
||
merge(...items: BagItems[]): this { | ||
const prevState = this.all(); | ||
const nextState = deepmerge.all<BagItems>([this.all(), ...items], { | ||
arrayMerge: (_destinationArray: unknown[], sourceArray: unknown[]) => sourceArray, | ||
isMergeableObject: (o: object) => isPlainArray(o) || isPlainObject(o) | ||
}); | ||
|
||
Object.keys(nextState).forEach((key) => { | ||
this[key] = nextState[key]; | ||
}); | ||
|
||
return invokeWatchers(this, nextState, prevState); | ||
} | ||
|
||
watch(path: string, onChange: WatchCallback, deep = false): this { | ||
return watch(this, path, onChange, deep); | ||
} | ||
|
||
unwatch(path: string, onChange: WatchCallback): this { | ||
return unwatch(this, path, onChange); | ||
} | ||
} |
Oops, something went wrong.