Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed behavior of MappingProxy.setValue function #155

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/typings/MappingProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class MappingProxy<T> extends StockProxy<T> {
defaultSetValue: <U>(path: Pxth<U>, value: SetStateAction<U>) => void,
defaultGetValue: <U>(path: Pxth<U>) => U,
) => {
const relativeValuePath = relativePxth(this.path as Pxth<unknown>, path as Pxth<unknown>);
const relativeValuePath = relativePxth(this.path, path);
const normalPath = this.getNormalPath(path);

if (hasMappedParentPaths(relativeValuePath, this.proxyMap)) {
Expand All @@ -45,16 +45,11 @@ export class MappingProxy<T> extends StockProxy<T> {
}

const innerPaths = getInnerPaths(relativeValuePath, this.proxyMap);

const oldValue = defaultGetValue(normalPath);
const newValue = isFunction(value) ? value(this.getValue(path, defaultGetValue)) : value;

innerPaths.forEach(
([to, from]) =>
from !== undefined &&
defaultSetValue(
from,
deepGet(isFunction(value) ? value(oldValue) : value, relativePxth(relativeValuePath, to)),
),
from !== undefined && defaultSetValue(from, deepGet(newValue, relativePxth(relativeValuePath, to))),
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/mappingProxyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export const getInnerPaths = <V>(path: Pxth<V>, proxyMap: PxthMap<Pxth<unknown>>
};

export const hasMappedParentPaths = <V>(path: Pxth<V>, proxyMap: PxthMap<Pxth<unknown>>) => {
return proxyMap.entries().some(([mappedPath]) => isInnerPxth(mappedPath, path as Pxth<unknown>));
return proxyMap.keys().some((mappedPath) => isInnerPxth(mappedPath, path as Pxth<unknown>));
};
29 changes: 19 additions & 10 deletions test/typings/MappingProxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createPxth, deepGet, deepSet, getPxthSegments, Pxth, samePxth } from 'p

import { MappingProxy, Observer, ProxyMapSource } from '../../src/typings';

// Fake object type
type RegisteredUser = {
registrationDate: Date;
personalData: {
Expand Down Expand Up @@ -287,14 +288,15 @@ describe('Mapping proxy', () => {
});

it('should set proxied value based on the old value', () => {
const proxy = new MappingProxy<RegisteredUser>(getUserMapSource(), createPxth(['registeredUser']));
const proxy = new MappingProxy<RegisteredUser>(getUserMapSource(), createPxth(['proxy']));

const defaultSetValue = jest.fn();
const getStringValue = jest.fn(() => 'old value');
let updater: jest.Mock<any, any> = jest.fn((old) => old + ' updated');

proxy.setValue(
createPxth(['registeredUser', 'personalData', 'name', 'firstName']),
(old) => old + ' updated',
createPxth(['proxy', 'personalData', 'name', 'firstName']),
updater,
defaultSetValue,
getStringValue as <U>(path: Pxth<U>) => U,
);
Expand All @@ -305,16 +307,17 @@ describe('Mapping proxy', () => {
]);
expect(getPxthSegments(defaultSetValue.mock.calls[0][0])).toStrictEqual(['registeredUser', 'name']);
expect(defaultSetValue).toBeCalledWith(expect.anything(), 'old value updated');
expect(updater).toBeCalledTimes(1);

defaultSetValue.mockClear();
const getObjectValue = jest.fn(() => ({ firstName: 'As', lastName: 'Df' })) as <U>(path: Pxth<U>) => U;
const getObjectValue = jest.fn(() => ({ name: 'As', surname: 'Df', dates: { registration: new Date() } })) as <
U,
>(
path: Pxth<U>,
) => U;
updater = jest.fn((old: object) => ({ ...old, lastName: 'updated' }));

proxy.setValue(
createPxth<object>(['registeredUser', 'personalData', 'name']),
(old: object) => ({ ...old, lastName: 'updated' }),
defaultSetValue,
getObjectValue,
);
proxy.setValue(createPxth<object>(['proxy', 'personalData', 'name']), updater, defaultSetValue, getObjectValue);

expect(
defaultSetValue.mock.calls.findIndex(
Expand All @@ -327,6 +330,12 @@ describe('Mapping proxy', () => {
([path, value]) => samePxth(path, createPxth(['registeredUser', 'surname'])) && value === 'updated',
) !== -1,
).toBeTruthy();

expect(updater).toBeCalledTimes(1);
expect(updater.mock.calls[0][0]).toStrictEqual({
firstName: 'As',
lastName: 'Df',
});
});

it('should get proxied value', () => {
Expand Down
Loading