File size: 4,228 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { startsWith } from 'lodash';
import {
	getLanguageFilePathUrl,
	getLanguageFileUrl,
	getLanguageManifestFileUrl,
	getLanguagesInternalBasePath,
	getTranslationChunkFileUrl,
} from 'calypso/lib/i18n-utils/switch-locale';

describe( 'getLanguageFileUrl()', () => {
	test( 'should return a JS url.', () => {
		const expected = getLanguageFilePathUrl() + 'ja-v1.1.js';

		expect( getLanguageFileUrl( 'ja', 'js' ) ).toEqual( expected );
	} );

	test( 'should return a JSON url.', () => {
		const expected = getLanguageFilePathUrl() + 'ja-v1.1.json';

		expect( getLanguageFileUrl( 'ja', 'json' ) ).toEqual( expected );
	} );

	test( 'should return a JSON url if an unknown fileType is given.', () => {
		const expected = getLanguageFilePathUrl() + 'ja-v1.1.json';

		expect( getLanguageFileUrl( 'ja', 'Profit!' ) ).toEqual( expected );
	} );

	test( 'should return a protocol-relative path under a browser context.', () => {
		let hasMockedWindow = false;

		if ( ! global.window ) {
			global.window = {};
			hasMockedWindow = true;
		}

		expect( startsWith( getLanguageFileUrl( 'ja' ), '//' ) ).toBe( true );

		if ( hasMockedWindow ) {
			global.window = null;
		}
	} );

	test( 'should append a revision cache buster.', () => {
		const expectedWithNumberRevision = getLanguageFilePathUrl() + 'zh-v1.1.js?v=123';

		expect( getLanguageFileUrl( 'zh', 'js', { zh: 123 } ) ).toEqual( expectedWithNumberRevision );

		const expectedWithStringRevision = getLanguageFilePathUrl() + 'de-v1.1.js?v=abc123';

		expect( getLanguageFileUrl( 'de', 'js', { de: 'abc123' } ) ).toEqual(
			expectedWithStringRevision
		);
	} );

	test( 'should not append a revision cache buster for an unknown locale.', () => {
		const expected = getLanguageFilePathUrl() + 'kr-v1.1.js';

		expect( getLanguageFileUrl( 'kr', 'js', { xd: 222 } ) ).toEqual( expected );
	} );

	test( 'should not use a non-number-or-string revision', () => {
		const expected = getLanguageFilePathUrl() + 'zh-v1.1.js';

		expect( getLanguageFileUrl( 'zh', 'js', { zh: true } ) ).toEqual( expected );
	} );
} );

describe( 'getLanguagesInternalBasePath()', () => {
	test( 'should return base path for languages.', () => {
		expect( getLanguagesInternalBasePath() ).toEqual( '/calypso/languages' );
	} );
} );

describe( 'getLanguageManifestFileUrl()', () => {
	test( 'should return language manifest url for a given locale.', () => {
		const expected = getLanguagesInternalBasePath() + '/ja-language-manifest.json';

		expect( getLanguageManifestFileUrl( { localeSlug: 'ja' } ) ).toEqual( expected );
	} );

	test( 'should append a revision cache buster.', () => {
		const hash = '123';
		const expected = `${ getLanguagesInternalBasePath() }/zh-language-manifest.json?v=${ hash }`;

		expect( getLanguageManifestFileUrl( { localeSlug: 'zh', hash } ) ).toEqual( expected );
	} );

	test( 'should not append a revision cache buster for an unknown locale.', () => {
		const expected = getLanguagesInternalBasePath() + '/kr-language-manifest.json';

		expect(
			getLanguageManifestFileUrl( { localeSlug: 'kr', languageRevisions: { xd: 222 } } )
		).toEqual( expected );
	} );
} );

describe( 'getTranslationChunkFileUrl()', () => {
	test( 'should return translation chunk url for a given locale.', () => {
		const localeSlug = 'ja';
		const chunkId = 'chunk-abc.min';
		const expected = `${ getLanguagesInternalBasePath() }/${ localeSlug }-${ chunkId }.json`;

		expect( getTranslationChunkFileUrl( { chunkId, localeSlug } ) ).toEqual( expected );
	} );

	test( 'should append a revision cache buster.', () => {
		const localeSlug = 'zh';
		const chunkId = 'chunk-abc.min';
		const hash = '123';
		const expected = `${ getLanguagesInternalBasePath() }/${ localeSlug }-${ chunkId }.json?v=${ hash }`;

		expect( getTranslationChunkFileUrl( { chunkId, localeSlug, hash } ) ).toEqual( expected );
	} );

	test( 'should not append a revision cache buster for an unknown locale.', () => {
		const localeSlug = 'kr';
		const chunkId = 'chunk-abc.min';
		const expected = `${ getLanguagesInternalBasePath() }/${ localeSlug }-${ chunkId }.json`;

		expect(
			getTranslationChunkFileUrl( { chunkId, localeSlug, languageRevisions: { xd: 222 } } )
		).toEqual( expected );
	} );
} );