PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.21
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.21
1.4.21 1.4.20 1.4.19 1.4.18 1.4.17 1.4.16 1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 All 184 releases
disco / backend / views / components / Main / pages / Campaigns / __test__ / Campaigns.test.jsx

Campaigns.test.jsx in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.21, at backend/views/components/Main/pages/Campaigns/__test__/Campaigns.test.jsx

351 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { fireEvent, screen, waitFor } from '@testing-library/dom';
2 import '@testing-library/jest-dom';
3 import '@wordpress/jest-console';
4 import 'whatwg-fetch';
5 import {
6 useAddCampaignMutation,
7 useDeleteCampaignMutation,
8 useGetCampaignsQuery,
9 usePatchCampaignMutation,
10 } from '../../../features/campaigns/campaignsApi';
11 import {
12 useGetBOGOTypesQuery,
13 useGetDiscountBasedOnQuery,
14 useGetDiscountIntentsQuery,
15 useGetDiscountTypesQuery,
16 useGetFiltersQuery,
17 useGetWhatGetsDiscountQuery,
18 } from '../../../features/discount/discountApi';
19 import { useGetSettingsQuery } from '../../../features/settings/settingsApi';
20 import {
21 dateStringToTimestamp,
22 dateTimeFormatter,
23 getSelectedFilterData,
24 prepareCampaignForRequest,
25 } from '../../../utilities/utilities';
26 import {
27 filters,
28 renderWithProviders,
29 testCampaigns,
30 } from '../../../utilities/utils-for-tests';
31 import Campaigns from '../Campaigns';
32
33 jest.mock('../../../features/campaigns/campaignsApi.js', () => ({
34 useGetCampaignsQuery: jest.fn(),
35 useAddCampaignMutation: jest.fn(),
36 usePatchCampaignMutation: jest.fn(),
37 useDeleteCampaignMutation: jest.fn(),
38 }));
39
40 jest.mock('../../../features/discount/discountApi', () => ({
41 useGetDiscountIntentsQuery: jest.fn(),
42 useGetFiltersQuery: jest.fn(),
43 useGetWhatGetsDiscountQuery: jest.fn(),
44 useGetDiscountMethodsQuery: jest.fn(),
45 useGetDiscountTypesQuery: jest.fn(),
46 useGetDiscountBasedOnQuery: jest.fn(),
47 useGetBOGOTypesQuery: jest.fn(),
48 }));
49 jest.mock('../../../features/settings/settingsApi', () => ({
50 useGetSettingsQuery: jest.fn(),
51 }));
52 jest.mock('../../../utilities/utilities', () => ({
53 prepareCampaignForRequest: jest.fn(),
54 dateTimeFormatter: jest.fn(),
55 getSelectedFilterData: jest.fn(),
56 dateStringToTimestamp: jest.fn(),
57 truncate: jest.fn((str) => str),
58 }));
59
60 // Mock react-router
61 jest.mock('react-router', () => {
62 return {
63 ...jest.requireActual('react-router'),
64 useNavigate: () => jest.fn(),
65 };
66 });
67
68 describe('Discount Campaigns Page', () => {
69 beforeEach(() => {
70 useGetCampaignsQuery.mockClear();
71 });
72
73 afterEach(() => {
74 useGetCampaignsQuery.mockClear();
75 });
76
77 URL.createObjectURL = jest.fn(() => 'mocked-url');
78
79 useGetCampaignsQuery.mockReturnValue({
80 data: testCampaigns,
81 isLoading: false,
82 isSuccess: true,
83 isError: false,
84 error: null,
85 });
86 const addCampaignMock = jest.fn();
87 useAddCampaignMutation.mockReturnValue([
88 addCampaignMock,
89 { isLoading: false, isSuccess: true, isError: false, error: null },
90 ]);
91
92 usePatchCampaignMutation.mockReturnValue([
93 jest.fn(),
94 {
95 data: testCampaigns[0],
96 isLoading: false,
97 isSuccess: true,
98 isError: false,
99 error: null,
100 },
101 ]);
102 useDeleteCampaignMutation.mockReturnValue([
103 jest.fn(),
104 { isLoading: false, isSuccess: true, isError: false, error: null },
105 ]);
106
107 useGetDiscountIntentsQuery.mockReturnValue({
108 data: {
109 name: 'discount_intents',
110 values: {
111 Product: 'Product',
112 Cart: 'Cart',
113 Shipping: 'Free Shipping',
114 Bulk: 'Bulk Discount',
115 Bundle: 'Bundle Discount',
116 BOGO: 'BOGO',
117 },
118 },
119 isLoading: false,
120 isSuccess: true,
121 isError: false,
122 error: null,
123 });
124
125 useGetFiltersQuery.mockReturnValue({
126 data: filters,
127 isLoading: false,
128 isSuccess: true,
129 isError: false,
130 error: null,
131 });
132 useGetWhatGetsDiscountQuery.mockReturnValue({
133 data: {
134 name: 'products',
135 values: {
136 all_products: 'All Products',
137 products: 'Few Products',
138 },
139 },
140 isLoading: false,
141 isSuccess: true,
142 isError: false,
143 error: null,
144 });
145
146 useGetDiscountTypesQuery.mockReturnValue({
147 data: {
148 name: 'discount_types',
149 values: {
150 percent: '% - Percentage',
151 fixed: '$ - Fixed',
152 percent_per_product: '% - Percentage Per Product',
153 fixed_per_product: '$ - Fixed Per Product',
154 free: 'Free',
155 },
156 },
157 isLoading: false,
158 isSuccess: true,
159 isError: false,
160 error: null,
161 });
162 useGetDiscountBasedOnQuery.mockReturnValue({
163 data: {
164 name: 'discount_based_on',
165 values: {
166 item_quantity: 'Item Quantity',
167 item_price: 'Item Price',
168 cart_quantity: 'Cart Quantity',
169 cart_subtotal: 'Cart Subtotal',
170 },
171 },
172 isLoading: false,
173 isSuccess: true,
174 isError: false,
175 error: null,
176 });
177 useGetBOGOTypesQuery.mockReturnValue({
178 data: {
179 name: 'bogo_types',
180 values: {
181 all: 'All',
182 products: 'Products',
183 categories: 'Categories',
184 },
185 },
186 isLoading: false,
187 isSuccess: true,
188 isError: false,
189 error: null,
190 });
191 useGetSettingsQuery.mockReturnValue({
192 data: { product_price_type: 'price', min_max_discount_amount: '50' },
193 isLoading: false,
194 isSuccess: true,
195 isError: false,
196 error: null,
197 });
198
199 prepareCampaignForRequest.mockReturnValue({});
200 dateTimeFormatter.mockReturnValue('');
201 getSelectedFilterData.mockReturnValue({});
202 dateStringToTimestamp.mockReturnValue('');
203
204 describe('Header', () => {
205 test('Render Correctly', async () => {
206 // Mock console.warn if specific warnings are causing the test to fail
207 const originalWarn = console.warn;
208 console.warn = (...args) => {
209 if (args[0].includes('React Router Future Flag Warning')) {
210 return;
211 }
212 originalWarn(...args);
213 };
214
215 // Render the component
216 renderWithProviders(<Campaigns />);
217
218 // Ensure elements are found and rendered correctly
219 const heading = await screen.findByText('Discount Campaigns'); // Use `findBy` to await presence if async
220 const importButton = screen.getByText('Import');
221 const createButton = screen.getByText('Create a Discount');
222 const settingsButton = screen.getByText('Settings');
223
224 // Assert that all expected elements are in the document
225 expect(heading).toBeInTheDocument();
226 expect(importButton).toBeInTheDocument();
227 expect(createButton).toBeInTheDocument();
228 expect(settingsButton).toBeInTheDocument();
229
230 // Restore the original console.warn after the test
231 console.warn = originalWarn;
232 });
233
234 test('Import Interact Correctly', async () => {
235 renderWithProviders(<Campaigns />);
236
237 const importInput =
238 screen.getByPlaceholderText('Select Disco File');
239 fireEvent.change(importInput, {
240 target: {
241 files: [
242 new File(['{"data": "test data"}'], 'test.disco', {
243 type: 'application/json',
244 }),
245 ],
246 },
247 });
248
249 await waitFor(() => {
250 expect(addCampaignMock).toHaveBeenCalled();
251 });
252 });
253
254 test('Create Discount Button Interact Correctly', async () => {
255 const mockNavigate = jest.fn();
256 const useNavigateMock = jest.spyOn(
257 require('react-router'),
258 'useNavigate'
259 );
260
261 useNavigateMock.mockImplementation(() => mockNavigate);
262
263 renderWithProviders(<Campaigns />);
264 const createButton = screen.getByText('Create a Discount');
265 fireEvent.click(createButton);
266 expect(mockNavigate).toHaveBeenCalledWith('disco');
267 });
268
269 test('Settings Button Interact Correctly', async () => {
270 const mockNavigate = jest.fn();
271 const useNavigateMock = jest.spyOn(
272 require('react-router'),
273 'useNavigate'
274 );
275
276 useNavigateMock.mockImplementation(() => mockNavigate);
277
278 renderWithProviders(<Campaigns />);
279 const settingButton = screen.getByText('Settings');
280 fireEvent.click(settingButton);
281 expect(mockNavigate).toHaveBeenCalledWith('settings');
282 });
283 });
284
285 describe('Action Bar', () => {
286 test('Render Correctly', () => {
287 renderWithProviders(<Campaigns />);
288
289 const bulkActionSelect = screen.getByText('Bulk Actions');
290 // const applyButton = screen.getByText('Apply');
291 const searchInput = screen.getByPlaceholderText('Search Campaign');
292 // const searchButton = screen.getByText('Search');
293
294 expect(bulkActionSelect).toBeInTheDocument();
295 // expect(applyButton).toBeInTheDocument();
296 expect(searchInput).toBeInTheDocument();
297 // expect(searchButton).toBeInTheDocument();
298 });
299
300 test('Search Work Correctly', () => {
301 renderWithProviders(<Campaigns />);
302 const searchInput = screen.getByPlaceholderText('Search Campaign');
303 // const searchButton = screen.getByText('Search');
304
305 fireEvent.change(searchInput, { target: { value: 'Winter' } });
306 // fireEvent.click(searchButton);
307 const percentDiscount = screen.queryByText('50% Off');
308 expect(percentDiscount).not.toBeInTheDocument();
309
310 const actionsButtons = screen.getAllByText('Actions');
311 expect(actionsButtons.length).toBe(1);
312 });
313 });
314
315 describe('Campaigns List', () => {
316 test('Render Correctly', () => {
317 renderWithProviders(<Campaigns />);
318
319 const nameColumn = screen.getByText('Campaign Name');
320 const actionColumn = screen.getByText('Actions');
321 const winterSaleCamp = screen.getByText('Winter Sale');
322 const percentDiscount = screen.getByText('50% Off');
323 // const actionsButtons = screen.getAllByText('Actions');
324
325 expect(nameColumn).toBeInTheDocument();
326 expect(actionColumn).toBeInTheDocument();
327 expect(winterSaleCamp).toBeInTheDocument();
328 expect(percentDiscount).toBeInTheDocument();
329 // expect(actionsButtons.length).toBe(2);
330 });
331
332 test('Name Click Navigate To Edit', () => {
333 const mockNavigate = jest.fn();
334 const useNavigateMock = jest.spyOn(
335 require('react-router'),
336 'useNavigate'
337 );
338
339 useNavigateMock.mockImplementation(() => mockNavigate);
340
341 renderWithProviders(<Campaigns />);
342
343 const winterSaleCamp = screen.getByText('Winter Sale');
344 fireEvent.click(winterSaleCamp);
345 expect(mockNavigate).toHaveBeenCalledWith('disco?edit=409', {
346 state: testCampaigns[1],
347 });
348 });
349 });
350 });
351