|
|
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(); |
|
|
} ); |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
expect( select( store ).getDomainState() ).toEqual( DataStatus.Uninitialized ); |
|
|
|
|
|
|
|
|
expect( select( store ).getDomainSuggestions( query, options ) ).toEqual( undefined ); |
|
|
|
|
|
await listenForStateUpdate(); |
|
|
|
|
|
|
|
|
expect( select( store ).getDomainState() ).toEqual( DataStatus.Pending ); |
|
|
|
|
|
await listenForStateUpdate(); |
|
|
|
|
|
|
|
|
expect( select( store ).getDomainState() ).toEqual( DataStatus.Success ); |
|
|
|
|
|
|
|
|
expect( select( store ).getDomainSuggestions( query, options ) ).toEqual( apiResponse ); |
|
|
|
|
|
await listenForStateUpdate(); |
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
} ); |
|
|
} ); |
|
|
}; |
|
|
|
|
|
|
|
|
expect( select( store ).getDomainErrorMessage() ).toBeFalsy(); |
|
|
|
|
|
|
|
|
select( store ).getDomainSuggestions( query, options ); |
|
|
await listenForStateUpdate(); |
|
|
|
|
|
|
|
|
select( store ).getDomainSuggestions( query, options ); |
|
|
await listenForStateUpdate(); |
|
|
|
|
|
|
|
|
expect( select( store ).getDomainErrorMessage() ).toBeTruthy(); |
|
|
} ); |
|
|
} ); |
|
|
|