Skip to content

Commit 10e57ca

Browse files
committed
docs(theme): update tests for CartSidebar
1 parent 52d5da8 commit 10e57ca

File tree

8 files changed

+427
-7
lines changed

8 files changed

+427
-7
lines changed

packages/theme/components/CartSidebar.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
v-else
133133
class="color-danger sf-badge__absolute"
134134
>
135-
<template>
135+
<template #default>
136136
<span>{{ $t('Out of stock') }}</span>
137137
</template>
138138
</SfBadge>
@@ -230,6 +230,7 @@
230230
</div>
231231
</template>
232232
<script>
233+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
233234
import {
234235
SfLoader,
235236
SfNotification,
@@ -344,7 +345,7 @@ export default defineComponent({
344345
return {
345346
sendToRemove,
346347
actionRemoveItem,
347-
loading,
348+
loading: computed(() => (!!loading.value)),
348349
isAuthenticated,
349350
products,
350351
removeItem,

packages/theme/components/SearchResults.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
>
3737
<SfMenuItem
3838
:label="category.label"
39-
:link="localePath(th.getAgnosticCatLink(category))"
39+
:link="th.getAgnosticCatLink(category)"
4040
>
4141
<template #mobile-nav-icon>
4242
&#8203;
@@ -76,7 +76,7 @@
7676
:image="productGetters.getProductThumbnailImage(product)"
7777
:alt="productGetters.getName(product)"
7878
:title="productGetters.getName(product)"
79-
:link="localePath(`/p/${productGetters.getProductSku(product)}${productGetters.getSlug(product, product.categories[0])}`)"
79+
:link="`/p/${productGetters.getProductSku(product)}${productGetters.getSlug(product, product.categories[0])}`"
8080
:wishlist-icon="isAuthenticated ? 'heart' : ''"
8181
:is-in-wishlist-icon="isAuthenticated ? 'heart_fill' : ''"
8282
:is-in-wishlist="product.isInWishlist"
@@ -96,7 +96,7 @@
9696
:image="productGetters.getProductThumbnailImage(product)"
9797
:alt="productGetters.getName(product)"
9898
:title="productGetters.getName(product)"
99-
:link="localePath(`/p/${productGetters.getProductSku(product)}${productGetters.getSlug(product, product.categories[0])}`)"
99+
:link="`/p/${productGetters.getProductSku(product)}${productGetters.getSlug(product, product.categories[0])}`"
100100
:wishlist-icon="isAuthenticated ? 'heart' : ''"
101101
:is-in-wishlist-icon="isAuthenticated ? 'heart_fill' : ''"
102102
:is-in-wishlist="product.isInWishlist"
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import userEvent from '@testing-library/user-event';
2+
import {
3+
useCart,
4+
useUser,
5+
} from '@vue-storefront/magento';
6+
7+
import { useUiState } from '~/composables';
8+
import {
9+
render, useCartMock, useUserMock, useUiStateMock, useEmptyCartMock,
10+
} from '~/test-utils';
11+
import CartSidebar from '~/components/CartSidebar';
12+
13+
jest.mock('@vue-storefront/magento', () => ({
14+
...jest.requireActual('@vue-storefront/magento'),
15+
useExternalCheckout: jest.fn(() => ({ initializeCheckout: {} })),
16+
useCart: jest.fn(),
17+
useUser: jest.fn(),
18+
}));
19+
20+
jest.mock('~/composables/useUiState');
21+
22+
useUser.mockReturnValue(useUserMock());
23+
useCart.mockReturnValue(useCartMock());
24+
25+
describe('<CartSidebar>', () => {
26+
it('should be not visible by default', () => {
27+
useUiState.mockReturnValue(useUiStateMock());
28+
const { queryByText } = render(CartSidebar);
29+
expect(queryByText('My Cart')).toBeNull();
30+
});
31+
32+
describe('If the cart is empty', () => {
33+
beforeAll(() => {
34+
useCart.mockReturnValue(useEmptyCartMock());
35+
});
36+
describe('And the cart sidebar is open', () => {
37+
it('should render empty state', () => {
38+
useUiState.mockReturnValue(useUiStateMock({ isCartSidebarOpen: true }));
39+
const { queryByText } = render(CartSidebar);
40+
expect(queryByText('Your cart is empty')).toBeTruthy();
41+
});
42+
43+
it('go back button must close the cart sidebar', () => {
44+
const uiStateMock = useUiStateMock({ isCartSidebarOpen: true });
45+
useUiState.mockReturnValue(uiStateMock);
46+
const { queryByText } = render(CartSidebar);
47+
const closeSidebarBtn = queryByText('Go back shopping');
48+
expect(closeSidebarBtn).toBeTruthy();
49+
50+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
51+
userEvent.click(closeSidebarBtn);
52+
expect(uiStateMock.toggleCartSidebar).toHaveBeenCalledTimes(1);
53+
});
54+
});
55+
});
56+
57+
describe('If the cart has two products', () => {
58+
beforeAll(() => {
59+
useCart.mockReturnValue(useCartMock());
60+
useUiState.mockReturnValue(useUiStateMock({ isCartSidebarOpen: true }));
61+
});
62+
63+
it('should render two Product Cards', () => {
64+
const { container } = render(CartSidebar);
65+
const productCards = container.querySelectorAll('div.sf-collected-product');
66+
expect(productCards).toHaveLength(2);
67+
});
68+
69+
it('should display proper total items value', () => {
70+
const { container } = render(CartSidebar);
71+
const totalItemsLValue = container.querySelector('div.cart-summary .sf-property__value');
72+
const totalItemsLabel = container.querySelector('div.cart-summary .sf-property__name');
73+
expect(totalItemsLabel).toHaveTextContent('Total items');
74+
expect(totalItemsLValue).toHaveTextContent(2);
75+
});
76+
77+
it('should render "go to checkout" button', () => {
78+
const { getByText } = render(CartSidebar);
79+
expect(getByText('Go to checkout')).toBeTruthy();
80+
});
81+
82+
it('should render promo code input', () => {
83+
const { getByTestId } = render(CartSidebar);
84+
expect(getByTestId('promoCode')).toBeTruthy();
85+
});
86+
87+
describe('And exactly one product is out of stock', () => {
88+
it('should display exactly one out of stock badge', () => {
89+
const { getAllByText } = render(CartSidebar);
90+
expect(getAllByText('Out of stock')).toHaveLength(1);
91+
});
92+
});
93+
});
94+
});

packages/theme/test-utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { render } from '@testing-library/vue';
22

33
const $t = (text) => text;
4-
4+
const $n = (text) => text;
5+
const localePath = (path) => path;
56
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
67
const customRender = (component, options = {}, callback = null) => render(component, {
78
mocks: {
89
$t,
10+
$n,
11+
localePath,
912
$nuxt: {
1013
context: {
1114
app: {
12-
localePath: (path) => path,
15+
localePath,
1316
},
1417
},
1518
},
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import stockStatusEnum from '~/enums/stockStatusEnum';
2+
3+
export const cartGettersMock = (extend = {}) => ({
4+
getItems: jest.fn(() => []),
5+
getTotals: jest.fn(() => 0),
6+
getTotalItems: jest.fn(),
7+
getStockStatus: jest.fn(() => stockStatusEnum.inStock),
8+
...extend,
9+
});
10+
11+
export default cartGettersMock;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export * from './useGuestUser';
22
export * from './useUser';
3+
export * from './useCart';
4+
export * from './useUiState';
5+
export * from './cartGetters';

0 commit comments

Comments
 (0)