first commit
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* External dependencies
|
||||
*/
|
||||
import { select, dispatch } from '@wordpress/data';
|
||||
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { hasNoticesOfType, removeNoticesByStatus } from '../notices';
|
||||
|
||||
jest.mock( '@wordpress/data' );
|
||||
|
||||
describe( 'Notice utils', () => {
|
||||
beforeEach( () => {
|
||||
jest.resetAllMocks();
|
||||
} );
|
||||
describe( 'hasNoticesOfType', () => {
|
||||
it( 'Correctly returns if there are notices of a given type in the core data store', () => {
|
||||
select.mockReturnValue( {
|
||||
getNotices: jest.fn().mockReturnValue( [
|
||||
{
|
||||
id: 'coupon-form',
|
||||
status: 'error',
|
||||
content:
|
||||
'Coupon cannot be removed because it is not already applied to the cart.',
|
||||
spokenMessage:
|
||||
'Coupon cannot be removed because it is not already applied to the cart.',
|
||||
isDismissible: true,
|
||||
actions: [],
|
||||
type: 'default',
|
||||
icon: null,
|
||||
explicitDismiss: false,
|
||||
},
|
||||
] ),
|
||||
} );
|
||||
const hasSnackbarNotices = hasNoticesOfType(
|
||||
'wc/cart',
|
||||
'snackbar'
|
||||
);
|
||||
const hasDefaultNotices = hasNoticesOfType( 'wc/cart', 'default' );
|
||||
expect( hasDefaultNotices ).toBe( true );
|
||||
expect( hasSnackbarNotices ).toBe( false );
|
||||
} );
|
||||
|
||||
it( 'Handles notices being empty', () => {
|
||||
select.mockReturnValue( {
|
||||
getNotices: jest.fn().mockReturnValue( [] ),
|
||||
} );
|
||||
const hasDefaultNotices = hasNoticesOfType( 'wc/cart', 'default' );
|
||||
expect( hasDefaultNotices ).toBe( false );
|
||||
} );
|
||||
} );
|
||||
describe( 'removeNoticesByStatus', () => {
|
||||
it( 'Correctly removes notices of a given status', () => {
|
||||
select.mockReturnValue( {
|
||||
getNotices: jest.fn().mockReturnValue( [
|
||||
{
|
||||
id: 'coupon-form',
|
||||
status: 'error',
|
||||
content:
|
||||
'Coupon cannot be removed because it is not already applied to the cart.',
|
||||
spokenMessage:
|
||||
'Coupon cannot be removed because it is not already applied to the cart.',
|
||||
isDismissible: true,
|
||||
actions: [],
|
||||
type: 'default',
|
||||
icon: null,
|
||||
explicitDismiss: false,
|
||||
},
|
||||
{
|
||||
id: 'address-form',
|
||||
status: 'error',
|
||||
content: 'Address invalid',
|
||||
spokenMessage: 'Address invalid',
|
||||
isDismissible: true,
|
||||
actions: [],
|
||||
type: 'default',
|
||||
icon: null,
|
||||
explicitDismiss: false,
|
||||
},
|
||||
{
|
||||
id: 'some-warning',
|
||||
status: 'warning',
|
||||
content: 'Warning notice.',
|
||||
spokenMessage: 'Warning notice.',
|
||||
isDismissible: true,
|
||||
actions: [],
|
||||
type: 'default',
|
||||
icon: null,
|
||||
explicitDismiss: false,
|
||||
},
|
||||
] ),
|
||||
} );
|
||||
dispatch.mockReturnValue( {
|
||||
removeNotice: jest.fn(),
|
||||
} );
|
||||
removeNoticesByStatus( 'error' );
|
||||
expect( dispatch().removeNotice ).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'coupon-form',
|
||||
''
|
||||
);
|
||||
expect( dispatch().removeNotice ).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'address-form',
|
||||
''
|
||||
);
|
||||
} );
|
||||
|
||||
it( 'Handles notices being empty', () => {
|
||||
select.mockReturnValue( {
|
||||
getNotices: jest.fn().mockReturnValue( [] ),
|
||||
} );
|
||||
|
||||
dispatch.mockReturnValue( {
|
||||
removeNotice: jest.fn(),
|
||||
} );
|
||||
removeNoticesByStatus( 'empty' );
|
||||
expect( dispatch().removeNotice ).not.toBeCalled();
|
||||
} );
|
||||
} );
|
||||
} );
|
||||
@@ -0,0 +1,84 @@
|
||||
/**
|
||||
* Internal dependencies
|
||||
*/
|
||||
import { getImageSrcFromProduct, getImageIdFromProduct } from '../products';
|
||||
|
||||
describe( 'getImageSrcFromProduct', () => {
|
||||
test( 'returns first image src', () => {
|
||||
const imageSrc = getImageSrcFromProduct( {
|
||||
images: [ { src: 'foo.jpg' } ],
|
||||
} );
|
||||
|
||||
expect( imageSrc ).toBe( 'foo.jpg' );
|
||||
} );
|
||||
|
||||
test( 'returns empty string if no product was provided', () => {
|
||||
const imageSrc = getImageSrcFromProduct();
|
||||
|
||||
expect( imageSrc ).toBe( '' );
|
||||
} );
|
||||
|
||||
test( 'returns empty string if product is empty', () => {
|
||||
const imageSrc = getImageSrcFromProduct( {} );
|
||||
|
||||
expect( imageSrc ).toBe( '' );
|
||||
} );
|
||||
|
||||
test( 'returns empty string if product has no images', () => {
|
||||
const imageSrc = getImageSrcFromProduct( { images: null } );
|
||||
|
||||
expect( imageSrc ).toBe( '' );
|
||||
} );
|
||||
|
||||
test( 'returns empty string if product has 0 images', () => {
|
||||
const imageSrc = getImageSrcFromProduct( { images: [] } );
|
||||
|
||||
expect( imageSrc ).toBe( '' );
|
||||
} );
|
||||
|
||||
test( 'returns empty string if product image has no src attribute', () => {
|
||||
const imageSrc = getImageSrcFromProduct( { images: [ {} ] } );
|
||||
|
||||
expect( imageSrc ).toBe( '' );
|
||||
} );
|
||||
} );
|
||||
|
||||
describe( 'getImageIdFromProduct', () => {
|
||||
test( 'returns first image id', () => {
|
||||
const imageUrl = getImageIdFromProduct( {
|
||||
images: [ { id: 123 } ],
|
||||
} );
|
||||
|
||||
expect( imageUrl ).toBe( 123 );
|
||||
} );
|
||||
|
||||
test( 'returns 0 if no product was provided', () => {
|
||||
const imageUrl = getImageIdFromProduct();
|
||||
|
||||
expect( imageUrl ).toBe( 0 );
|
||||
} );
|
||||
|
||||
test( 'returns 0 if product is empty', () => {
|
||||
const imageUrl = getImageIdFromProduct( {} );
|
||||
|
||||
expect( imageUrl ).toBe( 0 );
|
||||
} );
|
||||
|
||||
test( 'returns 0 if product has no images', () => {
|
||||
const imageUrl = getImageIdFromProduct( { images: null } );
|
||||
|
||||
expect( imageUrl ).toBe( 0 );
|
||||
} );
|
||||
|
||||
test( 'returns 0 if product has 0 images', () => {
|
||||
const imageUrl = getImageIdFromProduct( { images: [] } );
|
||||
|
||||
expect( imageUrl ).toBe( 0 );
|
||||
} );
|
||||
|
||||
test( 'returns 0 if product image has no src attribute', () => {
|
||||
const imageUrl = getImageIdFromProduct( { images: [ {} ] } );
|
||||
|
||||
expect( imageUrl ).toBe( 0 );
|
||||
} );
|
||||
} );
|
||||
Reference in New Issue
Block a user