UNPKG

1.63 kBJavaScriptView Raw
1import { useChannelBanner } from './useChannelBanner';
2import React from 'react';
3import { act, render } from '@testing-library/react';
4import { useCookies } from 'react-cookie';
5
6jest.mock('react-cookie');
7
8function mockedUseCookies() {
9 return [{}, () => {}];
10}
11
12useCookies.mockImplementation(mockedUseCookies);
13
14describe('useChannelBanner', () => {
15 test('sets the banner to open by default', () => {
16 const { hookValues } = setup();
17
18 expect(hookValues.bannerIsOpen).toBe(true);
19 });
20 test('sets banner to closed if there is a hideBanner cookie', () => {
21 useCookies.mockImplementation(() => {
22 return [
23 {
24 hideBanner: 'true'
25 },
26 () => {}
27 ];
28 });
29
30 const { hookValues } = setup();
31
32 expect(hookValues.bannerIsOpen).toBe(false);
33 });
34 describe('hideTheBanner', () => {
35 test('sets a cookie with the name of hideBanner and value true', () => {
36 let cookieName;
37 let value;
38
39 useCookies.mockImplementation(() => {
40 return [
41 {},
42 (a, b) => {
43 [cookieName, value] = [a, b];
44 }
45 ];
46 });
47
48 const { hookValues } = setup();
49
50 act(() => hookValues.hideTheBanner());
51
52 expect(cookieName).toBe('hideBanner');
53 expect(value).toBe(true);
54 });
55 });
56});
57
58function setup() {
59 let hookValues;
60
61 const utils = render(
62 <Component>
63 {returns => {
64 hookValues = returns;
65 return null;
66 }}
67 </Component>
68 );
69
70 return { ...utils, hookValues };
71}
72
73function Component({ children }) {
74 const returns = useChannelBanner();
75
76 return children(returns);
77}