1 | import { selectPriceType, usePrice } from './usePrice';
|
2 | import { render } from '@testing-library/react';
|
3 | import ChannelContext from '../components/ChannelContext';
|
4 | import React from 'react';
|
5 |
|
6 | describe('selectPriceType', () => {
|
7 | let includeVat;
|
8 | let priceObj;
|
9 | let initialPriceObj = {
|
10 | vat: 10,
|
11 | incVat: 30,
|
12 | exVat: 20
|
13 | };
|
14 |
|
15 | beforeEach(() => {
|
16 |
|
17 | priceObj = initialPriceObj;
|
18 | });
|
19 |
|
20 | describe('when includeVat is true', () => {
|
21 | beforeEach(() => {
|
22 | includeVat = true;
|
23 | });
|
24 | it('returns only the vat-included value', () => {
|
25 | expect(selectPriceType(includeVat, priceObj)).toMatchObject({
|
26 | value: 30
|
27 | });
|
28 | });
|
29 | describe('when the passed price object is undefined', () => {
|
30 | beforeEach(() => {
|
31 | priceObj = undefined;
|
32 | });
|
33 | it('returns null', () => {
|
34 | expect(selectPriceType(includeVat, priceObj)).toBe(null);
|
35 | });
|
36 | });
|
37 | });
|
38 |
|
39 | describe('when includeVat is false', () => {
|
40 | beforeEach(() => {
|
41 | includeVat = false;
|
42 | });
|
43 | it('returns the vat portion and the exVat value', () => {
|
44 | expect(selectPriceType(includeVat, priceObj)).toMatchObject({
|
45 | value: 20,
|
46 | vat: 10
|
47 | });
|
48 | });
|
49 | describe('when the passed price object is undefined', () => {
|
50 | it('returns an object with a null value', () => {
|
51 | priceObj = undefined;
|
52 | expect(selectPriceType(includeVat, priceObj)).toBe(null);
|
53 | });
|
54 | });
|
55 | });
|
56 | });
|
57 |
|
58 | const defaultOpts = {
|
59 | price: {
|
60 | incVat: 12498.75,
|
61 | exVat: 9999,
|
62 | vat: 2499.75
|
63 | }
|
64 | };
|
65 |
|
66 | describe('usePrice', () => {
|
67 | it('renders using a custom formatter if provided', () => {
|
68 | const { getByText } = setup({
|
69 | ...defaultOpts,
|
70 | formatter: price => (
|
71 | <div>
|
72 | <span>{price}</span>
|
73 | <span>hi</span>
|
74 | </div>
|
75 | )
|
76 | });
|
77 |
|
78 | expect(getByText('12498.75'));
|
79 | expect(getByText('hi'));
|
80 | });
|
81 | it('allows currency to be overridden', () => {
|
82 | const { getByText } = setup({
|
83 | ...defaultOpts,
|
84 | currencyOverride: { code: 'EUR', culture: 'en-IE' }
|
85 | });
|
86 |
|
87 | expect(getByText('€12,498.75'));
|
88 | });
|
89 | it('allows includeVat to be overriden', () => {
|
90 | const { getByText } = setup({ ...defaultOpts, includeVat: false });
|
91 |
|
92 | expect(getByText('SEK 9,999.00'));
|
93 | });
|
94 | it('sets `hasDiscount` when the price is discounted', () => {
|
95 | const { getByText: getByTextDefault } = setup({ ...defaultOpts });
|
96 | expect(getByTextDefault('no discount'));
|
97 |
|
98 | const { getByText } = setup({
|
99 | ...defaultOpts,
|
100 | previousPrice: { incVat: 50000.0, exVat: 42000.0, vat: 8000.0 }
|
101 | });
|
102 |
|
103 | expect(getByText('discounted'));
|
104 | });
|
105 | it('returns the currency code and culture', () => {
|
106 | const { getByText } = setup({ ...defaultOpts });
|
107 |
|
108 | expect(getByText('SEK'));
|
109 | expect(getByText('sv-SE'));
|
110 | });
|
111 | });
|
112 |
|
113 | function setup(opts = defaultOpts) {
|
114 | const channelCountries = [
|
115 | {
|
116 | id: 'AU',
|
117 | code: 'AU',
|
118 | name: 'Australia',
|
119 | isDefault: true
|
120 | },
|
121 | {
|
122 | code: 'NZ',
|
123 | id: 'NZ',
|
124 | name: 'New Zealand',
|
125 | isDefault: false
|
126 | }
|
127 | ];
|
128 | const utils = render(
|
129 | <ChannelContext.Provider
|
130 | value={{
|
131 | selectedChannel: {
|
132 | countries: channelCountries,
|
133 | settings: {
|
134 | pricesIncVat: true
|
135 | },
|
136 | currency: {
|
137 | id: 'SEK',
|
138 | name: 'SEK',
|
139 | isDefault: true,
|
140 | format: {
|
141 | code: 'SEK',
|
142 | culture: 'sv-SE',
|
143 | decimals: 2
|
144 | }
|
145 | }
|
146 | }
|
147 | }}
|
148 | >
|
149 | <Wrapper opts={opts} />
|
150 | </ChannelContext.Provider>
|
151 | );
|
152 |
|
153 | return { ...utils };
|
154 | }
|
155 |
|
156 | function Wrapper({ opts }) {
|
157 | const { currencyLocale, formattedPricing, hasDiscount } = usePrice(opts);
|
158 |
|
159 | return (
|
160 | <>
|
161 | <div>{formattedPricing.price}</div>
|
162 | <div>{hasDiscount ? 'discounted' : 'no discount'}</div>
|
163 | <div>
|
164 | <span>{currencyLocale.code}</span>
|
165 | <span>{currencyLocale.culture}</span>
|
166 | </div>
|
167 | </>
|
168 | );
|
169 | }
|