|
1 | 1 | import {AppHomeRow} from '.' |
2 | | -import {PreviewLink, QRCodeModal} from '..' |
3 | 2 | import {DefaultProviders} from 'tests/DefaultProviders' |
4 | | -import {Button} from '@/components' |
5 | 3 | import React from 'react' |
6 | | - |
7 | 4 | import {render, withProviders} from '@shopify/ui-extensions-test-utils' |
| 5 | +import {screen, fireEvent} from '@testing-library/react' |
8 | 6 |
|
9 | | -vi.mock('..', () => ({ |
10 | | - NotApplicable: () => null, |
11 | | - PreviewLink: () => null, |
12 | | - QRCodeModal: () => null, |
13 | | - Row: (props: any) => props.children, |
| 7 | +const {QRCodeModalMock, PreviewLinkMock} = vi.hoisted(() => ({ |
| 8 | + QRCodeModalMock: vi.fn(), |
| 9 | + PreviewLinkMock: vi.fn(), |
14 | 10 | })) |
15 | 11 |
|
16 | | -vi.mock('@/components', () => ({ |
17 | | - Button: (props: any) => props.children, |
| 12 | +vi.mock('..', () => ({ |
| 13 | + NotApplicable: () => null, |
| 14 | + PreviewLink: (props: any) => { |
| 15 | + PreviewLinkMock(props) |
| 16 | + return null |
| 17 | + }, |
| 18 | + QRCodeModal: (props: any) => { |
| 19 | + QRCodeModalMock(props) |
| 20 | + return <div data-testid="qr-code-modal" onClick={props.onClose} /> |
| 21 | + }, |
| 22 | + Row: ({children}: any) => <tr>{children}</tr>, |
18 | 23 | })) |
19 | 24 |
|
20 | 25 | describe('<AppHomeRow/>', () => { |
21 | 26 | const defaultState = { |
22 | 27 | app: {url: 'mock.url', title: 'Mock App Title'}, |
23 | 28 | } |
24 | 29 |
|
| 30 | + beforeEach(() => { |
| 31 | + QRCodeModalMock.mockClear() |
| 32 | + PreviewLinkMock.mockClear() |
| 33 | + }) |
| 34 | + |
25 | 35 | test('renders a <QRCodeModal/>, closed by default', () => { |
26 | | - const container = render(<AppHomeRow />, withProviders(DefaultProviders), {state: defaultState}) |
| 36 | + render(<AppHomeRow />, withProviders(DefaultProviders), {state: defaultState}) |
27 | 37 |
|
28 | | - expect(container).toContainReactComponent(QRCodeModal, {code: undefined}) |
| 38 | + expect(QRCodeModalMock).toHaveBeenLastCalledWith(expect.objectContaining({code: undefined})) |
29 | 39 | }) |
30 | 40 |
|
31 | 41 | test('Opens and closes the <QRCodeModal/> ', () => { |
32 | | - const container = render(<AppHomeRow />, withProviders(DefaultProviders), {state: defaultState}) |
| 42 | + render(<AppHomeRow />, withProviders(DefaultProviders), {state: defaultState}) |
33 | 43 |
|
34 | | - container.act(() => { |
35 | | - container.find(Button)?.trigger('onClick') |
36 | | - }) |
| 44 | + fireEvent.click(screen.getByText('View mobile')) |
37 | 45 |
|
38 | | - expect(container).toContainReactComponent(QRCodeModal, { |
39 | | - code: { |
40 | | - url: defaultState.app.url, |
41 | | - type: 'home', |
42 | | - title: defaultState.app.title, |
43 | | - }, |
44 | | - }) |
| 46 | + expect(QRCodeModalMock).toHaveBeenLastCalledWith( |
| 47 | + expect.objectContaining({ |
| 48 | + code: { |
| 49 | + url: defaultState.app.url, |
| 50 | + type: 'home', |
| 51 | + title: defaultState.app.title, |
| 52 | + }, |
| 53 | + }), |
| 54 | + ) |
45 | 55 |
|
46 | | - container.act(() => { |
47 | | - container.find(QRCodeModal)?.trigger('onClose') |
48 | | - }) |
| 56 | + fireEvent.click(screen.getByTestId('qr-code-modal')) |
49 | 57 |
|
50 | | - expect(container).toContainReactComponent(QRCodeModal, {code: undefined}) |
| 58 | + expect(QRCodeModalMock).toHaveBeenLastCalledWith(expect.objectContaining({code: undefined})) |
51 | 59 | }) |
52 | 60 |
|
53 | 61 | test("renders a <PreviewLink/> with the resource url set to the app's handle if the surface has been set to 'admin'", () => { |
54 | 62 | const appState = { |
55 | 63 | app: {url: 'mock.url', title: 'Mock App Title', handle: 'my-app-handle'}, |
56 | 64 | } |
57 | | - const container = render(<AppHomeRow />, withProviders(DefaultProviders), { |
| 65 | + render(<AppHomeRow />, withProviders(DefaultProviders), { |
58 | 66 | state: appState, |
59 | 67 | client: {options: {surface: 'admin'}}, |
60 | 68 | }) |
61 | 69 |
|
62 | | - expect(container).toContainReactComponent(PreviewLink, {resourceUrl: '/admin/apps/my-app-handle'}) |
| 70 | + expect(PreviewLinkMock).toHaveBeenLastCalledWith( |
| 71 | + expect.objectContaining({resourceUrl: '/admin/apps/my-app-handle'}), |
| 72 | + ) |
63 | 73 | }) |
64 | 74 |
|
65 | 75 | test("renders a <PreviewLink/> without a resource url if the surface has not been set to 'admin'", () => { |
66 | 76 | const appState = { |
67 | 77 | app: {url: 'mock.url', title: 'Mock App Title', handle: 'my-app-handle'}, |
68 | 78 | } |
69 | | - const container = render(<AppHomeRow />, withProviders(DefaultProviders), { |
| 79 | + render(<AppHomeRow />, withProviders(DefaultProviders), { |
70 | 80 | state: appState, |
71 | 81 | client: {options: {surface: 'checkout'}}, |
72 | 82 | }) |
73 | 83 |
|
74 | | - expect(container).toContainReactComponent(PreviewLink, {resourceUrl: undefined}) |
| 84 | + expect(PreviewLinkMock).toHaveBeenLastCalledWith(expect.objectContaining({resourceUrl: undefined})) |
75 | 85 | }) |
76 | 86 | }) |
0 commit comments