File size: 3,621 Bytes
1e92f2d |
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 |
import { select, subscribe } from '@wordpress/data';
import wpcomRequest from 'wpcom-proxy-request';
import { store } from '..';
import { DataStatus } from '../constants';
jest.mock( 'wpcom-proxy-request', () => ( {
__esModule: true,
default: jest.fn(),
requestAllBlogsAccess: jest.fn( () => Promise.resolve() ),
} ) );
const options = {
category_slug: undefined,
include_dotblogsubdomain: false,
include_wordpressdotcom: true,
locale: 'en',
quantity: 11,
};
const apiResponse = [
{
domain_name: 'test.site',
relevance: 1,
supports_privacy: true,
vendor: 'donuts',
match_reasons: [ 'tld-common', 'tld-exact' ],
product_id: 78,
product_slug: 'dotsite_domain',
cost: '$25',
raw_price: 25,
currency_code: 'USD',
unavailable: false,
},
{
domain_name: 'hot-test-site.com',
relevance: 1,
supports_privacy: true,
vendor: 'donuts',
match_reasons: [ 'tld-common' ],
product_id: 6,
product_slug: 'domain_reg',
cost: '$18',
raw_price: 18,
currency_code: 'USD',
unavailable: false,
},
];
beforeEach( () => {
( wpcomRequest as jest.Mock ).mockReset();
} );
describe( 'getDomainSuggestions', () => {
it( 'resolves the state via an API call and caches the resolver on success', async () => {
( wpcomRequest as jest.Mock ).mockResolvedValue( apiResponse );
const query = 'test query one';
const listenForStateUpdate = () => {
return new Promise< void >( ( resolve ) => {
const unsubscribe = subscribe( () => {
unsubscribe();
resolve();
} );
} );
};
// Status should be uninitialized before first call
expect( select( store ).getDomainState() ).toEqual( DataStatus.Uninitialized );
// First call returns undefined
expect( select( store ).getDomainSuggestions( query, options ) ).toEqual( undefined );
await listenForStateUpdate();
// Status should be pending while we wait for the response
expect( select( store ).getDomainState() ).toEqual( DataStatus.Pending );
await listenForStateUpdate();
// Status should be successful once we get the response
expect( select( store ).getDomainState() ).toEqual( DataStatus.Success );
// By the second call, the resolver will have resolved
expect( select( store ).getDomainSuggestions( query, options ) ).toEqual( apiResponse );
await listenForStateUpdate();
// The resolver should now be cached with an `isStarting` value of false
expect(
select( 'core/data' ).isResolving( store, 'getDomainSuggestions', [ query, options ] )
).toEqual( false );
} );
} );
describe( 'getDomainErrorMessage', () => {
it( 'should return null if no error message', () => {
expect( select( store ).getDomainErrorMessage() ).toEqual( null );
} );
it( 'should return correct error message if no suggestions are returned', async () => {
( wpcomRequest as jest.Mock ).mockResolvedValue( null );
const query = 'test query two';
const listenForStateUpdate = () => {
return new Promise< void >( ( resolve ) => {
const unsubscribe = subscribe( () => {
unsubscribe();
resolve();
} );
} );
};
// The error message should start off as a null value
expect( select( store ).getDomainErrorMessage() ).toBeFalsy();
// First call returns undefined
select( store ).getDomainSuggestions( query, options );
await listenForStateUpdate();
// By the second call, the resolver will have resolved
select( store ).getDomainSuggestions( query, options );
await listenForStateUpdate();
// The promise resolves null suggestions so we should now have an error message
expect( select( store ).getDomainErrorMessage() ).toBeTruthy();
} );
} );
|