Skip to content

Commit 2b25246

Browse files
committed
feat(apply): add DEV check
1 parent 420a3bc commit 2b25246

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/apply.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { create } from './create';
3333
export function apply<
3434
T extends object,
3535
F extends boolean = false,
36-
A extends ApplyOptions<F> = ApplyOptions<F>
36+
A extends ApplyOptions<F> = ApplyOptions<F>,
3737
>(state: T, patches: Patches, applyOptions?: A): ApplyResult<T, F, A> {
3838
let i: number;
3939
for (i = patches.length - 1; i >= 0; i -= 1) {
@@ -124,6 +124,15 @@ export function apply<
124124
});
125125
};
126126
if ((applyOptions as ApplyMutableOptions)?.mutable) {
127+
if (__DEV__) {
128+
if (
129+
Object.keys(applyOptions!).filter((key) => key !== 'mutable').length
130+
) {
131+
console.warn(
132+
'The "mutable" option is not allowed to be used with other options.'
133+
);
134+
}
135+
}
127136
mutate(state);
128137
return undefined as ApplyResult<T, F, A>;
129138
}

test/dev.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable consistent-return */
22
/* eslint-disable no-param-reassign */
33
/* eslint-disable @typescript-eslint/ban-ts-comment */
4-
import { create } from '../src';
4+
import { apply, create } from '../src';
55

66
test('custom shallow copy without checking in prod mode', () => {
77
global.__DEV__ = false;
@@ -88,3 +88,53 @@ test('custom shallow copy with checking in dev mode', () => {
8888
`"You can't use mark and patches or auto freeze together."`
8989
);
9090
});
91+
92+
test('check warn when apply patches with other options', () => {
93+
{
94+
global.__DEV__ = true;
95+
const baseState = { foo: { bar: 'test' } };
96+
const warn = console.warn;
97+
jest.spyOn(console, 'warn').mockImplementation(() => {});
98+
apply(
99+
baseState,
100+
[
101+
{
102+
op: 'replace',
103+
path: ['foo', 'bar'],
104+
value: 'test2',
105+
},
106+
],
107+
{
108+
mutable: true,
109+
enableAutoFreeze: true,
110+
}
111+
);
112+
expect(console.warn).toHaveBeenCalledWith(
113+
'The "mutable" option is not allowed to be used with other options.'
114+
);
115+
}
116+
{
117+
global.__DEV__ = true;
118+
const baseState = { foo: { bar: 'test' } };
119+
const warn = console.warn;
120+
jest.spyOn(console, 'warn').mockImplementation(() => {});
121+
apply(
122+
baseState,
123+
[
124+
{
125+
op: 'replace',
126+
path: ['foo', 'bar'],
127+
value: 'test2',
128+
},
129+
],
130+
{
131+
mutable: true,
132+
enableAutoFreeze: true,
133+
mark: () => {},
134+
}
135+
);
136+
expect(console.warn).toHaveBeenCalledWith(
137+
'The "mutable" option is not allowed to be used with other options.'
138+
);
139+
}
140+
});

0 commit comments

Comments
 (0)