-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
Campaigns.test.tsx
185 lines (157 loc) · 4.89 KB
/
Campaigns.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
/**
* Internal dependencies
*/
import { useCampaignTypes, useCampaigns } from '~/marketing/hooks';
import { Campaigns } from './Campaigns';
jest.mock( '~/marketing/hooks', () => ( {
useCampaigns: jest.fn(),
useCampaignTypes: jest.fn(),
} ) );
jest.mock( '~/marketing/components', () => {
const originalModule = jest.requireActual( '~/marketing/components' );
return {
__esModule: true,
...originalModule,
CreateNewCampaignModal: () => <div>Mocked CreateNewCampaignModal</div>,
};
} );
/**
* Create a test campaign data object.
*/
const createTestCampaign = ( programId: string ) => {
return {
id: `google-listings-and-ads|${ programId }`,
title: `Campaign ${ programId }`,
description: '',
cost: `USD 30`,
manageUrl: `https://wc1.test/wp-admin/admin.php?page=wc-admin&path=/google/dashboard&subpath=/campaigns/edit&programId=${ programId }`,
icon: 'https://woocommerce.com/wp-content/uploads/2021/06/woo-GoogleListingsAds-jworee.png',
channelName: 'Google Listings and Ads',
channelSlug: 'google-listings-and-ads',
};
};
describe( 'Campaigns component', () => {
it( 'renders a TablePlaceholder when loading is in progress', () => {
( useCampaigns as jest.Mock ).mockReturnValue( {
loading: true,
error: undefined,
data: undefined,
meta: undefined,
} );
( useCampaignTypes as jest.Mock ).mockReturnValue( {
loading: true,
} );
const { container } = render( <Campaigns /> );
const tablePlaceholder = container.querySelector(
'div.woocommerce-table__table.is-loading'
);
expect( tablePlaceholder ).toBeInTheDocument();
} );
it( 'renders an error message when loading is done and data is undefined', () => {
( useCampaigns as jest.Mock ).mockReturnValue( {
loading: false,
error: {},
data: undefined,
meta: undefined,
} );
( useCampaignTypes as jest.Mock ).mockReturnValue( {
loading: false,
} );
render( <Campaigns /> );
expect(
screen.getByText( 'An unexpected error occurred.' )
).toBeInTheDocument();
} );
it( 'renders an info message when loading is done and data is an empty array', () => {
( useCampaigns as jest.Mock ).mockReturnValue( {
loading: false,
error: undefined,
data: [],
meta: {
total: 0,
},
} );
( useCampaignTypes as jest.Mock ).mockReturnValue( {
loading: false,
} );
render( <Campaigns /> );
expect(
screen.getByText( 'Advertise with marketing campaigns' )
).toBeInTheDocument();
} );
it( 'renders a table with campaign info and without pagination when loading is done and data is an array with length no more than page size', () => {
( useCampaigns as jest.Mock ).mockReturnValue( {
loading: false,
error: undefined,
data: [ createTestCampaign( '1' ) ],
meta: {
total: 1,
},
} );
( useCampaignTypes as jest.Mock ).mockReturnValue( {
loading: false,
} );
const { container } = render( <Campaigns /> );
expect( screen.getByText( 'Campaign 1' ) ).toBeInTheDocument();
expect( screen.getByText( 'USD 30' ) ).toBeInTheDocument();
const pagination = container.querySelector( '.woocommerce-pagination' );
expect( pagination ).not.toBeInTheDocument();
} );
it( 'renders a table with campaign info and with pagination when loading is done and data is an array with length more than page size', async () => {
( useCampaigns as jest.Mock ).mockReturnValue( {
loading: false,
error: undefined,
data: [
createTestCampaign( '1' ),
createTestCampaign( '2' ),
createTestCampaign( '3' ),
createTestCampaign( '4' ),
createTestCampaign( '5' ),
createTestCampaign( '6' ),
],
meta: {
total: 6,
},
} );
( useCampaignTypes as jest.Mock ).mockReturnValue( {
loading: false,
} );
render( <Campaigns /> );
// Campaign info.
expect( screen.getByText( 'Campaign 1' ) ).toBeInTheDocument();
// Pagination.
expect( screen.getByText( 'Page 1 of 2' ) ).toBeInTheDocument();
// Click on the next button to go to next page.
await userEvent.click(
screen.getByRole( 'button', { name: 'Next Page' } )
);
// Campaign info in the second page.
expect( screen.getByText( 'Campaign 6' ) ).toBeInTheDocument();
} );
it( 'renders a "Create new campaign" button in the card header, and upon clicking, displays the "Create a new campaign" modal', async () => {
( useCampaigns as jest.Mock ).mockReturnValue( {
loading: false,
error: undefined,
data: [ createTestCampaign( '1' ) ],
meta: {
total: 1,
},
} );
( useCampaignTypes as jest.Mock ).mockReturnValue( {
loading: false,
} );
render( <Campaigns /> );
await userEvent.click(
screen.getByRole( 'button', { name: 'Create new campaign' } )
);
// Mocked CreateNewCampaignModal should be displayed.
expect(
screen.getByText( 'Mocked CreateNewCampaignModal' )
).toBeInTheDocument();
} );
} );