|
| 1 | +/** @format */ |
| 2 | + |
| 3 | +/** |
| 4 | + * External dependencies |
| 5 | + */ |
| 6 | +import React from 'react'; |
| 7 | +import { mount } from 'enzyme'; |
| 8 | + |
| 9 | +describe( 'StoreNotices', () => { |
| 10 | + let createNotice; |
| 11 | + let removeNotices; |
| 12 | + let StoreNotices; |
| 13 | + let existingNotices; |
| 14 | + |
| 15 | + const usCart = { shippingAddress: { country: 'US', state: 'CA', postcode: '94105' } }; |
| 16 | + const caCart = { shippingAddress: { country: 'CA', state: 'ON', postcode: 'M5V 2T6' } }; |
| 17 | + |
| 18 | + const zipNotices = { |
| 19 | + 'woocommerce-services': { |
| 20 | + notices: [ { type: 'error', message: 'ZIP could not be validated.' } ], |
| 21 | + }, |
| 22 | + }; |
| 23 | + |
| 24 | + beforeEach( () => { |
| 25 | + jest.resetModules(); |
| 26 | + |
| 27 | + createNotice = jest.fn(); |
| 28 | + removeNotices = jest.fn(); |
| 29 | + existingNotices = []; |
| 30 | + |
| 31 | + global.window.wp = { |
| 32 | + element: { useEffect: React.useEffect }, |
| 33 | + data: { |
| 34 | + useDispatch: () => ( { createNotice, removeNotices } ), |
| 35 | + useSelect: ( selector ) => selector( () => ( { getNotices: () => existingNotices } ) ), |
| 36 | + }, |
| 37 | + }; |
| 38 | + |
| 39 | + ( { StoreNotices } = require( '../store-notices' ) ); |
| 40 | + } ); |
| 41 | + |
| 42 | + afterEach( () => { |
| 43 | + delete global.window.wp; |
| 44 | + } ); |
| 45 | + |
| 46 | + it( 'creates notices from the extension data for US addresses', () => { |
| 47 | + mount( <StoreNotices extensions={ zipNotices } cart={ usCart } /> ); |
| 48 | + |
| 49 | + // The id prefix is the contract the removal path relies on to find our own |
| 50 | + // notices, so it is asserted rather than ignored. |
| 51 | + expect( createNotice ).toHaveBeenCalledWith( |
| 52 | + 'error', |
| 53 | + 'ZIP could not be validated.', |
| 54 | + expect.objectContaining( { id: 'wcservices-store-notices-0', context: 'wc/cart' } ) |
| 55 | + ); |
| 56 | + } ); |
| 57 | + |
| 58 | + it( 'does not create notices for non-US addresses', () => { |
| 59 | + mount( <StoreNotices extensions={ zipNotices } cart={ caCart } /> ); |
| 60 | + |
| 61 | + expect( createNotice ).not.toHaveBeenCalled(); |
| 62 | + } ); |
| 63 | + |
| 64 | + it( 'removes only its own notices for US addresses', () => { |
| 65 | + existingNotices = [ |
| 66 | + { id: 'wcservices-store-notices-0' }, |
| 67 | + { id: 'some-other-plugin-notice' }, |
| 68 | + ]; |
| 69 | + |
| 70 | + mount( <StoreNotices extensions={ {} } cart={ usCart } /> ); |
| 71 | + |
| 72 | + expect( removeNotices ).toHaveBeenCalledWith( [ 'wcservices-store-notices-0' ], 'wc/cart' ); |
| 73 | + } ); |
| 74 | + |
| 75 | + it( 'does not remove notices for non-US addresses', () => { |
| 76 | + existingNotices = [ { id: 'wcservices-store-notices-0' } ]; |
| 77 | + |
| 78 | + mount( <StoreNotices extensions={ {} } cart={ caCart } /> ); |
| 79 | + |
| 80 | + expect( removeNotices ).not.toHaveBeenCalled(); |
| 81 | + } ); |
| 82 | + |
| 83 | + it( 'creates notices again when new extension data arrives', () => { |
| 84 | + const wrapper = mount( <StoreNotices extensions={ zipNotices } cart={ usCart } /> ); |
| 85 | + |
| 86 | + wrapper.setProps( { |
| 87 | + extensions: { |
| 88 | + 'woocommerce-services': { |
| 89 | + notices: [ { type: 'error', message: 'Address could not be validated.' } ], |
| 90 | + }, |
| 91 | + }, |
| 92 | + } ); |
| 93 | + |
| 94 | + expect( createNotice ).toHaveBeenCalledTimes( 2 ); |
| 95 | + expect( createNotice ).toHaveBeenLastCalledWith( |
| 96 | + 'error', |
| 97 | + 'Address could not be validated.', |
| 98 | + expect.objectContaining( { id: 'wcservices-store-notices-0', context: 'wc/cart' } ) |
| 99 | + ); |
| 100 | + } ); |
| 101 | + |
| 102 | + it( 'renders without crashing when the extension data is missing', () => { |
| 103 | + // Old WooCommerce versions register the blocks integration but not the |
| 104 | + // Store API extension, so extensions has no woocommerce-services key. |
| 105 | + expect( () => { |
| 106 | + mount( <StoreNotices extensions={ {} } cart={ usCart } /> ); |
| 107 | + } ).not.toThrow(); |
| 108 | + |
| 109 | + expect( createNotice ).not.toHaveBeenCalled(); |
| 110 | + } ); |
| 111 | + |
| 112 | + it( 'renders without crashing when the notices key is missing', () => { |
| 113 | + expect( () => { |
| 114 | + mount( <StoreNotices extensions={ { 'woocommerce-services': {} } } cart={ usCart } /> ); |
| 115 | + } ).not.toThrow(); |
| 116 | + |
| 117 | + expect( createNotice ).not.toHaveBeenCalled(); |
| 118 | + } ); |
| 119 | + |
| 120 | + it( 'renders without crashing when the notices data is not a list', () => { |
| 121 | + // Anything on the Store API response is outside our control; a non-list must not |
| 122 | + // reach forEach and take down the checkout fill. |
| 123 | + expect( () => { |
| 124 | + mount( |
| 125 | + <StoreNotices |
| 126 | + extensions={ { 'woocommerce-services': { notices: {} } } } |
| 127 | + cart={ usCart } |
| 128 | + /> |
| 129 | + ); |
| 130 | + } ).not.toThrow(); |
| 131 | + |
| 132 | + expect( createNotice ).not.toHaveBeenCalled(); |
| 133 | + } ); |
| 134 | +} ); |
0 commit comments