Skip to content

Commit

Permalink
Adds unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
supernova-at committed Jun 15, 2020
1 parent c3f85c7 commit 95be85f
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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(<Component />);

// 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(<Component />);

// Assert.
const { setIsCartUpdating } = log.mock.calls[0][0];
expect(setIsCartUpdating).toBeCalledWith(false);
});

0 comments on commit 95be85f

Please sign in to comment.