Skip to content

Commit

Permalink
readd a test for dimension key
Browse files Browse the repository at this point in the history
  • Loading branch information
dancerphil committed Dec 18, 2023
1 parent 44c960a commit 93cddc7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
23 changes: 11 additions & 12 deletions src/__test__/mappedRegion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ describe('createMappedRegion', () => {
expect(region.getValue('key')).toBe('Michael Jackson');
});

// TODO reopen after https://github.com/ecomfe/reskript/issues/271
// test('createMappedRegion with dimension key', () => {
// const region = createMappedRegion();
// region.set({x: 0, y: 0}, 0);
// region.set({x: 1, y: 1}, 2);
// expect(region.getValue({x: 0, y: 0})).toBe(0);
// expect(region.getValue({x: 1, y: 1})).toBe(2);
// region.set({x: 0, y: 0}, 1);
// expect(region.getValue({x: 0, y: 0})).toBe(1);
// region.reset({x: 0, y: 0});
// expect(region.getValue({x: 0, y: 0})).toBe(undefined);
// });
test('createMappedRegion with dimension key', () => {
const region = createMappedRegion();
region.set({x: 0, y: 0}, 0);
region.set({x: 1, y: 1}, 2);
expect(region.getValue({x: 0, y: 0})).toBe(0);
expect(region.getValue({x: 1, y: 1})).toBe(2);
region.set({x: 0, y: 0}, 1);
expect(region.getValue({x: 0, y: 0})).toBe(1);
region.reset({x: 0, y: 0});
expect(region.getValue({x: 0, y: 0})).toBe(undefined);
});

test('createMappedRegion loadBy key function', async () => {
const region = createMappedRegion<string, string>();
Expand Down
13 changes: 7 additions & 6 deletions src/createMappedRegion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ function createMappedRegion <K, V>(initialValue: V | void | undefined, option?:
return initialValue as V;
};

const getKeyString = (key: K | K[]): string => {
const getKeyString = (key: K): string => {
if (typeof key === 'string') {
return key;
}
// TODO remove after https://github.com/ecomfe/reskript/issues/271
// @ts-expect-error
// istanbul ignore next
return key === undefined ? key : jsonStableStringify(key);
if (key === undefined) {
return key as string;
}
return jsonStableStringify(key);
};
/* -------- */

Expand Down Expand Up @@ -309,7 +309,7 @@ function createMappedRegion <K, V>(initialValue: V | void | undefined, option?:
) => {
const loadByReturnFunction = async (params?: TParams): Promise<void> => {
// @ts-expect-error
const loadKey = typeof key === 'function' ? key(params as TParams) : key;
const loadKey: K = typeof key === 'function' ? key(params as TParams) : key;
const keyString = getKeyString(loadKey);

const promiseQueue = ref.promiseQueue.get(keyString);
Expand Down Expand Up @@ -443,6 +443,7 @@ function createMappedRegion <K, V>(initialValue: V | void | undefined, option?:
};

const useData: Result['useData'] = <TResult>(key: K, selector?: (value: V) => TResult) => {
console.warn('useData is deprecated since it is hardly maintained. Use useValue instead.');
const subscription = useValueSelectorSubscription(key, selector);
const currentPromise = useMemo(
() => getPromise(key),
Expand Down
2 changes: 2 additions & 0 deletions src/util/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type Listener = (e: StorageEvent) => void;
const createStorageEventListenerHook = () => {
const listeners = new Set<Listener>();
const handleStorageEvent = (e: StorageEvent) => {
// find some utils to test storage event
// istanbul ignore next
listeners.forEach(listener => {
listener(e);
});
Expand Down

0 comments on commit 93cddc7

Please sign in to comment.