From 95be85fe151ec2158f5d69ae999b9f5650437d94 Mon Sep 17 00:00:00 2001 From: Andrew Terranova Date: Mon, 15 Jun 2020 15:04:10 -0500 Subject: [PATCH] Adds unit tests --- .../CartPage/__tests__/useCartPage.spec.js | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/packages/peregrine/lib/talons/CartPage/__tests__/useCartPage.spec.js b/packages/peregrine/lib/talons/CartPage/__tests__/useCartPage.spec.js index 25ac065627..52dd175881 100644 --- a/packages/peregrine/lib/talons/CartPage/__tests__/useCartPage.spec.js +++ b/packages/peregrine/lib/talons/CartPage/__tests__/useCartPage.spec.js @@ -1,8 +1,19 @@ -import React, { useEffect } from 'react'; +import React, { useEffect, useState } from 'react'; import { createTestInstance } from '@magento/peregrine'; +import { useQuery } from '@apollo/react-hooks'; import { useCartPage } from '../useCartPage'; +jest.mock('react', () => { + const React = jest.requireActual('react'); + const spy = jest.spyOn(React, 'useState'); + + return { + ...React, + useState: spy + }; +}); + jest.mock('@apollo/react-hooks', () => { const queryResult = { called: false, @@ -70,3 +81,39 @@ test('it returns the proper shape', () => { shouldShowLoadingIndicator: expect.any(Boolean) }); }); + +test('it calls setIsCartUpdating true when loading is true', () => { + // Arrange. + useQuery.mockReturnValueOnce({ + called: true, + data: { cart: { total_quantity: 0 } }, + loading: true + }); + // isCartUpdating + useState.mockReturnValueOnce([false, jest.fn()]); + + // Act. + createTestInstance(); + + // Assert. + const { setIsCartUpdating } = log.mock.calls[0][0]; + expect(setIsCartUpdating).toBeCalledWith(true); +}); + +test('it calls setIsCartUpdating false when loading is false', () => { + // Arrange. + useQuery.mockReturnValueOnce({ + called: true, + data: { cart: { total_quantity: 0 } }, + loading: false + }); + // isCartUpdating + useState.mockReturnValueOnce([false, jest.fn()]); + + // Act. + createTestInstance(); + + // Assert. + const { setIsCartUpdating } = log.mock.calls[0][0]; + expect(setIsCartUpdating).toBeCalledWith(false); +});