File size: 2,303 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import {
	SITE_PLANS_FETCH_COMPLETED,
	SITE_PRODUCTS_FETCH_COMPLETED,
} from 'calypso/state/action-types';
import { serialize, deserialize } from 'calypso/state/utils';
import currencyCode from '../reducer';

describe( '#currencyCode()', () => {
	test( 'should default to null', () => {
		const state = currencyCode( undefined, {} );
		expect( state ).toBeNull();
	} );
	test( 'should return current state when we have empty site plans', () => {
		const state = currencyCode( 'USD', {
			type: SITE_PLANS_FETCH_COMPLETED,
			plans: [],
		} );
		expect( state ).toBe( 'USD' );
	} );
	test( 'should set currency code when site plans are received', () => {
		const state = currencyCode( undefined, {
			type: SITE_PLANS_FETCH_COMPLETED,
			plans: [
				{
					productName: 'Free',
					currencyCode: 'USD',
				},
			],
		} );
		expect( state ).toBe( 'USD' );
	} );
	test( 'should update currency code when site plans are received', () => {
		const state = currencyCode( 'USD', {
			type: SITE_PLANS_FETCH_COMPLETED,
			plans: [
				{
					productName: 'Free',
					currencyCode: 'CAD',
				},
			],
		} );
		expect( state ).toBe( 'CAD' );
	} );
	test( 'should set currency code when site products fetch is completed', () => {
		const state = currencyCode( undefined, {
			type: SITE_PRODUCTS_FETCH_COMPLETED,
			products: {
				free_plan: {
					product_name: 'WordPress.com Free',
					currency_code: 'USD',
				},
			},
		} );
		expect( state ).toBe( 'USD' );
	} );
	test( 'should update currency code when site products fetch is completed', () => {
		const state = currencyCode( 'USD', {
			type: SITE_PRODUCTS_FETCH_COMPLETED,
			products: {
				free_plan: {
					product_name: 'WordPress.com Free',
					currency_code: 'CAD',
				},
			},
		} );
		expect( state ).toBe( 'CAD' );
	} );
	test( 'should persist state', () => {
		const original = 'JPY';
		const state = serialize( currencyCode, original );
		expect( state ).toBe( original );
	} );
	test( 'should restore valid persisted state', () => {
		const original = 'JPY';
		const state = deserialize( currencyCode, original );
		expect( state ).toBe( original );
	} );
	test( 'should ignore invalid persisted state', () => {
		const original = 1234;
		const state = deserialize( currencyCode, original );
		expect( state ).toBeNull();
	} );
} );