Skip to content

Commit 9ce310a

Browse files
authored
fix: Uncaught TypeError: Do not know how to serialize a BigInt #42 (#43)
* fix: Uncaught TypeError: Do not know how to serialize a BigInt #42 * make Copied.test cases consistent
1 parent d98d1c1 commit 9ce310a

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

core/src/comps/Copied.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export const Copied = <T extends object, K extends TagType>(props: CopiedProps<T
3737
} else if (value instanceof Date) {
3838
copyText = value.toLocaleString();
3939
} else {
40-
copyText = JSON.stringify(value, null, 2);
40+
copyText = JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? v + 'n' : v), 2);
4141
}
4242
onCopied && onCopied(copyText, value);
4343
setCopied(true);

core/src/section/Copied.test.tsx

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ const avatar = 'https://i.imgur.com/MK3eW3As.jpg';
88
const example = {
99
avatar,
1010
};
11+
const exampleWithBigInt = {
12+
avatar,
13+
bigint: BigInt(1000),
14+
};
1115

12-
it('renders <JsonView /> Copied test case', async () => {
16+
// BigInt(1000) should render to '1000n'
17+
const exampleWithBigIntAnswer = {
18+
avatar,
19+
bigint: BigInt(1000).toString() + 'n',
20+
};
21+
22+
it('render <JsonView />, copy String test case', async () => {
1323
const user = userEvent.setup();
1424
// Mock the necessary functions and values
1525
const writeTextMock = jest.fn().mockResolvedValue(undefined);
@@ -54,7 +64,7 @@ it('renders <JsonView /> Copied test case', async () => {
5464
jest.restoreAllMocks();
5565
});
5666

57-
it('renders <JsonView /> Copy number test case', async () => {
67+
it('render <JsonView />, copy Number test case', async () => {
5868
const user = userEvent.setup();
5969

6070
// Mock the necessary functions and values
@@ -77,7 +87,7 @@ it('renders <JsonView /> Copy number test case', async () => {
7787
jest.restoreAllMocks();
7888
});
7989

80-
it('renders <JsonView.Copied /> render test case', async () => {
90+
it('render <JsonView.Copied />, copy Number test case', async () => {
8191
const user = userEvent.setup();
8292

8393
// Mock the necessary functions and values
@@ -107,7 +117,7 @@ it('renders <JsonView.Copied /> render test case', async () => {
107117
jest.restoreAllMocks();
108118
});
109119

110-
it('renders <JsonView.Copied /> copy NaN test case', async () => {
120+
it('render <JsonView.Copied />, copy NaN test case', async () => {
111121
const user = userEvent.setup();
112122

113123
// Mock the necessary functions and values
@@ -129,7 +139,7 @@ it('renders <JsonView.Copied /> copy NaN test case', async () => {
129139
jest.restoreAllMocks();
130140
});
131141

132-
it('renders <JsonView.Copied /> copy Infinity test case', async () => {
142+
it('render <JsonView.Copied />, copy Infinity test case', async () => {
133143
const user = userEvent.setup();
134144

135145
// Mock the necessary functions and values
@@ -151,7 +161,7 @@ it('renders <JsonView.Copied /> copy Infinity test case', async () => {
151161
jest.restoreAllMocks();
152162
});
153163

154-
it('renders <JsonView.Copied /> copy Infinity test case', async () => {
164+
it('render <JsonView.Copied />, copy BigInt test case', async () => {
155165
const user = userEvent.setup();
156166

157167
// Mock the necessary functions and values
@@ -176,3 +186,26 @@ it('renders <JsonView.Copied /> copy Infinity test case', async () => {
176186
expect(bigint.nextElementSibling).toBeNull();
177187
jest.restoreAllMocks();
178188
});
189+
190+
it('render <JsonView.Copied />, copy Object with BigInt test case', async () => {
191+
const user = userEvent.setup();
192+
193+
// Mock the necessary functions and values
194+
const writeTextMock = jest.fn().mockResolvedValue(undefined);
195+
jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock);
196+
const { container, debug } = render(
197+
<JsonView value={exampleWithBigInt}>
198+
<JsonView.Copied data-testid="copied" />
199+
<JsonView.CountInfo data-testid="countInfo" />
200+
</JsonView>,
201+
);
202+
expect(container.firstElementChild).toBeInstanceOf(Element);
203+
fireEvent.mouseEnter(container.lastElementChild!);
204+
const copied = screen.getByTestId('copied');
205+
await user.click(copied);
206+
// Assertions
207+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(JSON.stringify(exampleWithBigIntAnswer, null, 2));
208+
await user.unhover(container.lastElementChild!);
209+
// Restore the original implementation
210+
jest.restoreAllMocks();
211+
});

0 commit comments

Comments
 (0)