1 | import React from 'react';
|
2 | import { renderHook } from '@testing-library/react-hooks';
|
3 | import { useVariantFromUrl } from './useVariantFromUrl';
|
4 | import { createMemoryHistory } from 'history';
|
5 | import { Router } from 'react-router';
|
6 |
|
7 | function MockHistory({ children, initialEntries = ['/'] }) {
|
8 | const history = createMemoryHistory({ initialEntries });
|
9 |
|
10 | return <Router history={history}>{children}</Router>;
|
11 | }
|
12 |
|
13 | describe('useVariantFromUrl', () => {
|
14 | it('returns an empty object if there is no `att` param', () => {
|
15 | const MockWithParams = props => (
|
16 | <MockHistory initialEntries={['/yo?wow=hey']} {...props} />
|
17 | );
|
18 |
|
19 | const { result } = renderHook(() => useVariantFromUrl(), {
|
20 | wrapper: MockWithParams
|
21 | });
|
22 |
|
23 | expect(result.current).toEqual({});
|
24 | });
|
25 | it('returns an array with a single value', () => {
|
26 | const MockWithParams = props => (
|
27 | <MockHistory initialEntries={['/yo?att=ODAwIG1t']} {...props} />
|
28 | );
|
29 |
|
30 | const { result } = renderHook(() => useVariantFromUrl(), {
|
31 | wrapper: MockWithParams
|
32 | });
|
33 |
|
34 | expect(result.current.values).toEqual(['800 mm']);
|
35 | });
|
36 | it('returns an array of values when the encoded string is separated by pipes', () => {
|
37 |
|
38 |
|
39 | const MockWithParams = props => (
|
40 | <MockHistory
|
41 | initialEntries={['/yo?att=ODAwIG1tfDkwMCBtbQ==']}
|
42 | {...props}
|
43 | />
|
44 | );
|
45 |
|
46 | const { result } = renderHook(() => useVariantFromUrl(), {
|
47 | wrapper: MockWithParams
|
48 | });
|
49 |
|
50 | expect(result.current.values).toEqual(['800 mm', '900 mm']);
|
51 | });
|
52 |
|
53 | it('return multpiple encoded array values', () => {
|
54 | const MockWithParams = props => (
|
55 | <MockHistory initialEntries={['/hihi?att=R3LDpXw0NTAgbW0=']} {...props} />
|
56 | );
|
57 |
|
58 | const { result } = renderHook(() => useVariantFromUrl(), {
|
59 | wrapper: MockWithParams
|
60 | });
|
61 |
|
62 | expect(result.current.values).toEqual(['Grå', '450 mm']);
|
63 | });
|
64 |
|
65 | it('return encoded array value', () => {
|
66 | const MockWithParams = props => (
|
67 | <MockHistory
|
68 | initialEntries={['/horsemeup?att=SmFnIMOkciBlbiDDtmvDpG5kIGjDpHN0C==']}
|
69 | {...props}
|
70 | />
|
71 | );
|
72 |
|
73 | const { result } = renderHook(() => useVariantFromUrl(), {
|
74 | wrapper: MockWithParams
|
75 | });
|
76 |
|
77 | expect(result.current.values).toEqual(['Jag är en ökänd häst']);
|
78 | });
|
79 | });
|