Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhtungdu committed Oct 17, 2023
1 parent 4e56c8c commit 42b59c4
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions assets/js/blocks/mini-cart/utils/test/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import { getByTestId, waitFor } from '@testing-library/dom';
import { getSettingWithCoercion } from '@woocommerce/settings';
import apiFetch from '@wordpress/api-fetch';

/**
* Internal dependencies
Expand All @@ -17,23 +18,20 @@ import {

// This is a simplified version of the response of the Cart API endpoint.
const responseMock = {
ok: true,
json: async () => ( {
totals: {
total_price: '1800',
total_items: '1400',
total_items_tax: '200',
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
items_count: 2,
} ),
} as Response;
totals: {
total_price: '1800',
total_items: '1400',
total_items_tax: '200',
currency_code: 'USD',
currency_symbol: '$',
currency_minor_unit: 2,
currency_decimal_separator: '.',
currency_thousand_separator: ',',
currency_prefix: '$',
currency_suffix: '',
},
items_count: 2,
};
const localStorageMock = {
totals: {
total_price: '1800',
Expand Down Expand Up @@ -80,6 +78,8 @@ jest.mock( '@woocommerce/settings', () => {
};
} );

jest.mock( '@wordpress/api-fetch' );

describe( 'Mini-Cart frontend script when "the display prices during cart and checkout" option is set to "Including Tax"', () => {
beforeAll( () => {
( getSettingWithCoercion as jest.Mock ).mockReturnValue( true );
Expand Down Expand Up @@ -110,17 +110,17 @@ describe( 'Mini-Cart frontend script when "the display prices during cart and ch
} );

it( 'updates the cart contents based on the API response', async () => {
jest.spyOn( window, 'fetch' ).mockResolvedValue( responseMock );
apiFetch.mockResolvedValue( responseMock );
const container = getMiniCartDOM();
document.body.appendChild( container );

getMiniCartTotalsFromServer().then( updateTotals );

// Assert we called the correct endpoint.
await waitFor( () =>
expect( window.fetch ).toHaveBeenCalledWith(
'/wp-json/wc/store/v1/cart/'
)
expect( apiFetch ).toHaveBeenCalledWith( {
path: '/wc/store/v1/cart',
} )
);

// Assert we saved the values returned to the localStorage.
Expand Down Expand Up @@ -172,17 +172,17 @@ describe( 'Mini-Cart frontend script when "the display prices during cart and ch
} );

it( 'updates the cart contents based on the API response', async () => {
jest.spyOn( window, 'fetch' ).mockResolvedValue( responseMock );
apiFetch.mockResolvedValue( responseMock );
const container = getMiniCartDOM();
document.body.appendChild( container );

getMiniCartTotalsFromServer().then( updateTotals );

// Assert we called the correct endpoint.
await waitFor( () =>
expect( window.fetch ).toHaveBeenCalledWith(
'/wp-json/wc/store/v1/cart/'
)
expect( apiFetch ).toHaveBeenCalledWith( {
path: '/wc/store/v1/cart',
} )
);

// Assert we saved the values returned to the localStorage.
Expand Down

0 comments on commit 42b59c4

Please sign in to comment.