Skip to content

Commit

Permalink
test(symbol): add Symbol key testing
Browse files Browse the repository at this point in the history
  • Loading branch information
unadlib committed Dec 25, 2023
1 parent 6d8600e commit 02ad490
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
Binary file modified benchmark-object.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3785,3 +3785,81 @@ test(`'markSimpleObject' add null check`, () => {

expect(state).toEqual(obj);
});

test('object with Symbol key at root - 0', () => {
const a = Symbol('a');
const data: Record<PropertyKey, any> = {
[a]: 'str',
};

const state = create(data, (draft) => {
expect(draft[a]).toBe('str');
draft.foobar = 'str';
});
expect(state).toEqual({
...data,
foobar: 'str',
});
});

test('object with Symbol key at root - 1', () => {
const a = Symbol('a');
const data: Record<PropertyKey, any> = {
[a]: 'str',
};

const state = create(data, (draft) => {
draft.foobar = 'str';
expect(draft[a]).toBe('str');
});
expect(state).toEqual({
...data,
foobar: 'str',
});
});

test('object with non-enumerable Symbol key at root - 0', () => {
const a = Symbol('a');
const data: Record<PropertyKey, any> = {};

Object.defineProperty(data, a, {
value: 'str',
enumerable: false,
});

const state = create(data, (draft) => {
draft.foobar = 'str';
expect(draft[a]).toBeUndefined();
});
expect(state).toEqual({
...data,
foobar: 'str',
});
expect(state).toEqual({
foobar: 'str',
});
});

test('object with non-enumerable Symbol key at root - 1', () => {
const a = Symbol('a');
const data: Record<PropertyKey, any> = {};

Object.defineProperty(data, a, {
value: 'str',
enumerable: false,
});

const state = create(data, (draft) => {
// it's aligned with immer
expect(draft[a]).not.toBeUndefined();
expect(draft[a]).toBe('str');
draft.foobar = 'str';
});
expect(state).toEqual({
...data,
foobar: 'str',
});
expect(state).toEqual({
foobar: 'str',
});
});

0 comments on commit 02ad490

Please sign in to comment.