Skip to content

Commit

Permalink
fix: Uncaught TypeError: Do not know how to serialize a BigInt #42 (#43)
Browse files Browse the repository at this point in the history
* fix: Uncaught TypeError: Do not know how to serialize a BigInt #42

* make Copied.test cases consistent
  • Loading branch information
gregfenton committed Mar 29, 2024
1 parent d98d1c1 commit 9ce310a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion core/src/comps/Copied.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const Copied = <T extends object, K extends TagType>(props: CopiedProps<T
} else if (value instanceof Date) {
copyText = value.toLocaleString();
} else {
copyText = JSON.stringify(value, null, 2);
copyText = JSON.stringify(value, (_, v) => (typeof v === 'bigint' ? v + 'n' : v), 2);
}
onCopied && onCopied(copyText, value);
setCopied(true);
Expand Down
45 changes: 39 additions & 6 deletions core/src/section/Copied.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ const avatar = 'https://i.imgur.com/MK3eW3As.jpg';
const example = {
avatar,
};
const exampleWithBigInt = {
avatar,
bigint: BigInt(1000),
};

it('renders <JsonView /> Copied test case', async () => {
// BigInt(1000) should render to '1000n'
const exampleWithBigIntAnswer = {
avatar,
bigint: BigInt(1000).toString() + 'n',
};

it('render <JsonView />, copy String test case', async () => {
const user = userEvent.setup();
// Mock the necessary functions and values
const writeTextMock = jest.fn().mockResolvedValue(undefined);
Expand Down Expand Up @@ -54,7 +64,7 @@ it('renders <JsonView /> Copied test case', async () => {
jest.restoreAllMocks();
});

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

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

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

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

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

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

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

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

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

// Mock the necessary functions and values
Expand All @@ -176,3 +186,26 @@ it('renders <JsonView.Copied /> copy Infinity test case', async () => {
expect(bigint.nextElementSibling).toBeNull();
jest.restoreAllMocks();
});

it('render <JsonView.Copied />, copy Object with BigInt test case', async () => {
const user = userEvent.setup();

// Mock the necessary functions and values
const writeTextMock = jest.fn().mockResolvedValue(undefined);
jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock);
const { container, debug } = render(
<JsonView value={exampleWithBigInt}>
<JsonView.Copied data-testid="copied" />
<JsonView.CountInfo data-testid="countInfo" />
</JsonView>,
);
expect(container.firstElementChild).toBeInstanceOf(Element);
fireEvent.mouseEnter(container.lastElementChild!);
const copied = screen.getByTestId('copied');
await user.click(copied);
// Assertions
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(JSON.stringify(exampleWithBigIntAnswer, null, 2));
await user.unhover(container.lastElementChild!);
// Restore the original implementation
jest.restoreAllMocks();
});

0 comments on commit 9ce310a

Please sign in to comment.