|
| 1 | +import userEvent from '@testing-library/user-event'; |
| 2 | +import { screen, render, waitFor, fireEvent } from '@testing-library/react'; |
| 3 | +import JsonView from '../'; |
| 4 | +import React from 'react'; |
| 5 | +import { act } from 'react-test-renderer'; |
| 6 | + |
| 7 | +const avatar = 'https://i.imgur.com/MK3eW3As.jpg'; |
| 8 | +const example = { |
| 9 | + avatar, |
| 10 | +}; |
| 11 | + |
| 12 | +it('renders <JsonView /> Copied test case', async () => { |
| 13 | + const user = userEvent.setup(); |
| 14 | + |
| 15 | + // Mock the necessary functions and values |
| 16 | + const writeTextMock = jest.fn().mockResolvedValue(undefined); |
| 17 | + jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock); |
| 18 | + const divref = React.createRef<HTMLDivElement>(); |
| 19 | + const { container } = render( |
| 20 | + <JsonView |
| 21 | + value={example} |
| 22 | + ref={divref} |
| 23 | + // onCopied={(copyText, value) => { |
| 24 | + // console.log('>>>', copyText, value) |
| 25 | + // }} |
| 26 | + > |
| 27 | + <JsonView.Copied data-testid="copied" /> |
| 28 | + <JsonView.CountInfo data-testid="countInfo" /> |
| 29 | + </JsonView>, |
| 30 | + ); |
| 31 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 32 | + // await user.hover(container.lastElementChild!); |
| 33 | + fireEvent.mouseEnter(container.lastElementChild!); |
| 34 | + const copied = screen.getByTestId('copied'); |
| 35 | + expect(copied.style).toHaveProperty('height', '1em'); |
| 36 | + expect(copied.style).toHaveProperty('width', '1em'); |
| 37 | + expect(copied.style).toHaveProperty('cursor', 'pointer'); |
| 38 | + expect(copied.style).toHaveProperty('vertical-align', 'middle'); |
| 39 | + expect(copied.style).toHaveProperty('margin-left', '5px'); |
| 40 | + expect(copied.getAttribute('fill')).toEqual('var(--w-rjv-copied-color, currentColor)'); |
| 41 | + expect(copied.tagName).toEqual('svg'); |
| 42 | + await user.click(copied); |
| 43 | + act(() => { |
| 44 | + expect(copied.getAttribute('fill')).toEqual('var(--w-rjv-copied-success-color, #28a745)'); |
| 45 | + }); |
| 46 | + // Assertions |
| 47 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith(JSON.stringify(example, null, 2)); |
| 48 | + await user.unhover(container.lastElementChild!); |
| 49 | + const countInfo = screen.getByTestId('countInfo'); |
| 50 | + expect(countInfo.nextElementSibling).toBeNull(); |
| 51 | + await waitFor(() => { |
| 52 | + expect(divref.current instanceof HTMLDivElement).toBeTruthy(); |
| 53 | + }); |
| 54 | + // Restore the original implementation |
| 55 | + jest.restoreAllMocks(); |
| 56 | +}); |
| 57 | + |
| 58 | +it('renders <JsonView /> Copy number test case', async () => { |
| 59 | + const user = userEvent.setup(); |
| 60 | + |
| 61 | + // Mock the necessary functions and values |
| 62 | + const writeTextMock = jest.fn().mockResolvedValue(undefined); |
| 63 | + jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock); |
| 64 | + const { container } = render( |
| 65 | + <JsonView value={{ value: 123 }}> |
| 66 | + <JsonView.Copied data-testid="copied" /> |
| 67 | + <JsonView.Quote data-testid="quote" /> |
| 68 | + </JsonView>, |
| 69 | + ); |
| 70 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 71 | + const quote = screen.getAllByTestId('quote')[0]; |
| 72 | + const lineDom = quote.parentElement?.parentElement!; |
| 73 | + fireEvent.mouseEnter(lineDom); |
| 74 | + const copied = screen.getAllByTestId('copied')[1]; |
| 75 | + expect(copied.tagName).toEqual('svg'); |
| 76 | + await user.click(copied); |
| 77 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('123'); |
| 78 | + jest.restoreAllMocks(); |
| 79 | +}); |
| 80 | + |
| 81 | +it('renders <JsonView.Copied /> render test case', async () => { |
| 82 | + const user = userEvent.setup(); |
| 83 | + |
| 84 | + // Mock the necessary functions and values |
| 85 | + const writeTextMock = jest.fn().mockResolvedValue(undefined); |
| 86 | + jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock); |
| 87 | + const { container } = render( |
| 88 | + <JsonView value={{ value: 123 }}> |
| 89 | + <JsonView.Copied |
| 90 | + data-testid="copied" |
| 91 | + render={(props) => { |
| 92 | + return <span {...props}>xx</span>; |
| 93 | + }} |
| 94 | + /> |
| 95 | + <JsonView.Quote data-testid="quote" /> |
| 96 | + </JsonView>, |
| 97 | + ); |
| 98 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 99 | + const quote = screen.getAllByTestId('quote')[0]; |
| 100 | + const lineDom = quote.parentElement?.parentElement!; |
| 101 | + fireEvent.mouseEnter(lineDom); |
| 102 | + const copied = screen.getAllByTestId('copied')[1]; |
| 103 | + expect(copied.tagName).toEqual('SPAN'); |
| 104 | + expect(copied.innerHTML).toEqual('xx'); |
| 105 | + await user.click(copied); |
| 106 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('123'); |
| 107 | + jest.restoreAllMocks(); |
| 108 | +}); |
| 109 | + |
| 110 | +it('renders <JsonView.Copied /> copy NaN test case', async () => { |
| 111 | + const user = userEvent.setup(); |
| 112 | + |
| 113 | + // Mock the necessary functions and values |
| 114 | + const writeTextMock = jest.fn().mockResolvedValue(undefined); |
| 115 | + jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock); |
| 116 | + const { container } = render( |
| 117 | + <JsonView value={{ value: NaN }}> |
| 118 | + <JsonView.Copied data-testid="copied" /> |
| 119 | + <JsonView.Quote data-testid="quote" /> |
| 120 | + </JsonView>, |
| 121 | + ); |
| 122 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 123 | + const quote = screen.getAllByTestId('quote')[0]; |
| 124 | + const lineDom = quote.parentElement?.parentElement!; |
| 125 | + fireEvent.mouseEnter(lineDom); |
| 126 | + const copied = screen.getAllByTestId('copied')[1]; |
| 127 | + await user.click(copied); |
| 128 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('NaN'); |
| 129 | + jest.restoreAllMocks(); |
| 130 | +}); |
| 131 | + |
| 132 | +it('renders <JsonView.Copied /> copy Infinity test case', async () => { |
| 133 | + const user = userEvent.setup(); |
| 134 | + |
| 135 | + // Mock the necessary functions and values |
| 136 | + const writeTextMock = jest.fn().mockResolvedValue(undefined); |
| 137 | + jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock); |
| 138 | + const { container } = render( |
| 139 | + <JsonView value={{ value: Infinity }}> |
| 140 | + <JsonView.Copied data-testid="copied" /> |
| 141 | + <JsonView.Quote data-testid="quote" /> |
| 142 | + </JsonView>, |
| 143 | + ); |
| 144 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 145 | + const quote = screen.getAllByTestId('quote')[0]; |
| 146 | + const lineDom = quote.parentElement?.parentElement!; |
| 147 | + fireEvent.mouseEnter(lineDom); |
| 148 | + const copied = screen.getAllByTestId('copied')[1]; |
| 149 | + await user.click(copied); |
| 150 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('Infinity'); |
| 151 | + jest.restoreAllMocks(); |
| 152 | +}); |
| 153 | + |
| 154 | +it('renders <JsonView.Copied /> copy Infinity test case', async () => { |
| 155 | + const user = userEvent.setup(); |
| 156 | + |
| 157 | + // Mock the necessary functions and values |
| 158 | + const writeTextMock = jest.fn().mockResolvedValue(undefined); |
| 159 | + jest.spyOn(navigator.clipboard, 'writeText').mockImplementation(writeTextMock); |
| 160 | + const { container, debug } = render( |
| 161 | + <JsonView value={{ value: BigInt(1000) }}> |
| 162 | + <JsonView.Copied data-testid="copied" /> |
| 163 | + <JsonView.Quote data-testid="quote" /> |
| 164 | + </JsonView>, |
| 165 | + ); |
| 166 | + expect(container.firstElementChild).toBeInstanceOf(Element); |
| 167 | + const quote = screen.getAllByTestId('quote')[0]; |
| 168 | + const lineDom = quote.parentElement?.parentElement!; |
| 169 | + fireEvent.mouseEnter(lineDom); |
| 170 | + const copied = screen.getAllByTestId('copied')[1]; |
| 171 | + await user.click(copied); |
| 172 | + expect(navigator.clipboard.writeText).toHaveBeenCalledWith('1000n'); |
| 173 | + jest.restoreAllMocks(); |
| 174 | +}); |
0 commit comments