File size: 10,351 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
import {
addLocaleToPath,
getLanguage,
getLocaleFromPath,
isDefaultLocale,
removeLocaleFromPath,
isLocaleVariant,
localizeUrl,
canBeTranslated,
getPathParts,
filterLanguageRevisions,
translationExists,
isMagnificentLocale,
} from '@automattic/i18n-utils';
import { getWpI18nLocaleSlug } from '@automattic/i18n-utils/src/locale-context';
jest.mock( '@automattic/i18n-utils/src/locale-context', () => {
const originalModule = jest.requireActual( '@automattic/i18n-utils/src/locale-context' );
return {
__esModule: true,
...originalModule,
getWpI18nLocaleSlug: jest.fn( () => '' ),
};
} );
jest.mock( '@automattic/calypso-config', () => ( key ) => {
if ( 'i18n_default_locale_slug' === key ) {
return 'en';
}
if ( 'support_site_locales' === key ) {
return [ 'en', 'es', 'de', 'ja', 'pt-br' ];
}
if ( 'forum_locales' === key ) {
return [ 'en', 'es', 'de', 'ja', 'pt-br', 'th' ];
}
if ( 'magnificent_non_en_locales' === key ) {
return [
'es',
'pt-br',
'de',
'fr',
'he',
'ja',
'it',
'nl',
'ru',
'tr',
'id',
'zh-cn',
'zh-tw',
'ko',
'ar',
'sv',
];
}
if ( 'jetpack_com_locales' === key ) {
return [
'en',
'ar',
'de',
'es',
'fr',
'he',
'id',
'it',
'ja',
'ko',
'nl',
'pt-br',
'ro',
'ru',
'sv',
'tr',
'zh-cn',
'zh-tw',
];
}
} );
describe( 'utils', () => {
describe( '#isDefaultLocale', () => {
test( 'should return false when a non-default locale provided', () => {
expect( isDefaultLocale( 'fr' ) ).toEqual( false );
} );
test( 'should return true when a default locale provided', () => {
expect( isDefaultLocale( 'en' ) ).toEqual( true );
} );
} );
describe( '#addLocaleToPath', () => {
test( 'adds a locale to the path', () => {
expect( addLocaleToPath( '/start/flow/step', 'fr' ) ).toEqual( '/start/flow/step/fr' );
} );
test( 'adds a locale to the path, replacing any previous locale', () => {
expect( addLocaleToPath( '/start/flow/step/de', 'fr' ) ).toEqual( '/start/flow/step/fr' );
} );
test( 'adds a locale to the path, keeping query string intact', () => {
expect( addLocaleToPath( '/start/flow/step?foo=bar', 'fr' ) ).toEqual(
'/start/flow/step/fr?foo=bar'
);
} );
} );
describe( '#removeLocaleFromPath', () => {
test( 'should remove the :lang part of the URL', () => {
expect( removeLocaleFromPath( '/start/fr' ) ).toEqual( '/start' );
expect( removeLocaleFromPath( '/start/flow/fr' ) ).toEqual( '/start/flow' );
expect( removeLocaleFromPath( '/start/flow/step' ) ).toEqual( '/start/flow/step' );
} );
test( 'should remove the :lang part of the URL, keeping any query string', () => {
expect( removeLocaleFromPath( '/log-in/pl?foo=bar' ) ).toEqual( '/log-in?foo=bar' );
expect( removeLocaleFromPath( '/start/flow/step/fr?foo=bar' ) ).toEqual(
'/start/flow/step?foo=bar'
);
} );
test( 'should not change the URL if no lang is present', () => {
expect( removeLocaleFromPath( '/log-in' ) ).toEqual( '/log-in' );
expect( removeLocaleFromPath( '/start/flow/step?foo=bar' ) ).toEqual(
'/start/flow/step?foo=bar'
);
} );
test( 'should not remove the :flow part of the URL', () => {
expect( removeLocaleFromPath( '/start' ) ).toEqual( '/start' );
expect( removeLocaleFromPath( '/start/flow' ) ).toEqual( '/start/flow' );
} );
test( 'should not remove the :step part of the URL', () => {
expect( removeLocaleFromPath( '/start/flow/step' ) ).toEqual( '/start/flow/step' );
} );
test( 'should not remove keys from an invite', () => {
expect( removeLocaleFromPath( '/accept-invite/site.wordpress.com/123456/es' ) ).toEqual(
'/accept-invite/site.wordpress.com/123456'
);
expect(
removeLocaleFromPath( '/accept-invite/site.wordpress.com/123456/123456/123456/es' )
).toEqual( '/accept-invite/site.wordpress.com/123456/123456/123456' );
} );
} );
describe( '#getLocaleFromPath', () => {
test( 'should return undefined when no locale at end of path', () => {
expect( getLocaleFromPath( '/start' ) ).toBeUndefined();
} );
test( 'should return locale string when at end of path', () => {
expect( getLocaleFromPath( '/start/es' ) ).toEqual( 'es' );
expect(
getLocaleFromPath( '/accept-invite/site.wordpress.com/123456/123456/123456/es' )
).toEqual( 'es' );
} );
test( 'should correctly handle paths with query string', () => {
expect( getLocaleFromPath( '/start/es?query=string' ) ).toEqual( 'es' );
} );
} );
describe( '#getLanguage', () => {
test( 'should return a language', () => {
expect( getLanguage( 'ja' ).langSlug ).toEqual( 'ja' );
} );
test( 'should return a language with a country code', () => {
expect( getLanguage( 'pt-br' ).langSlug ).toEqual( 'pt-br' );
} );
test( 'should fall back to the language code when a country code is not available', () => {
expect( getLanguage( 'fr-zz' ).langSlug ).toEqual( 'fr' );
} );
test( 'should return undefined when no arguments are given', () => {
//note that removeLocaleFromPath is dependant on getLanguage returning undefined in this case.
expect( getLanguage() ).toBeUndefined();
} );
test( 'should return undefined when the locale is invalid', () => {
//note that removeLocaleFromPath is dependant on getLanguage returning undefined in this case.
expect( getLanguage( 'zz' ) ).toBeUndefined();
} );
test( 'should return undefined when we lookup random words', () => {
expect( getLanguage( 'themes' ) ).toBeUndefined();
expect( getLanguage( 'log-in' ) ).toBeUndefined();
} );
test( 'should return a language with a three letter country code', () => {
expect( getLanguage( 'ast' ).langSlug ).toEqual( 'ast' );
} );
test( 'should return the variant', () => {
expect( getLanguage( 'de_formal' ).langSlug ).toEqual( 'de_formal' );
} );
test( 'should return the parent slug since the given variant does not exist', () => {
expect( getLanguage( 'fr_formal' ).langSlug ).toEqual( 'fr' );
} );
} );
describe( '#isLocaleVariant', () => {
test( 'should return false by default', () => {
expect( isLocaleVariant( 'lol' ) ).toEqual( false );
expect( isLocaleVariant() ).toEqual( false );
} );
test( 'should detect a locale variant', () => {
expect( isLocaleVariant( 'de_formal' ) ).toEqual( true );
} );
test( 'should detect a root language', () => {
expect( isLocaleVariant( 'de' ) ).toEqual( false );
} );
} );
describe( '#canBeTranslated', () => {
test( 'should return true by default', () => {
expect( canBeTranslated() ).toEqual( true );
} );
test( 'should return false for elements in the exception list', () => {
expect( canBeTranslated( 'en' ) ).toEqual( false );
expect( canBeTranslated( 'sr_latin' ) ).toEqual( false );
} );
test( 'should return true for languages not in the exception list', () => {
expect( canBeTranslated( 'de' ) ).toEqual( true );
} );
} );
describe( '#localizeUrl', () => {
test( 'localizeUrl is still provided by client/lib/i18n-utils', () => {
expect( localizeUrl( 'https://wordpress.com/', 'de' ) ).toEqual(
'https://wordpress.com/de/'
);
} );
test( 'localizeUrl still uses getLocaleSlug', () => {
getWpI18nLocaleSlug.mockImplementationOnce( () => 'en' );
expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual(
'https://wordpress.com/support/'
);
getWpI18nLocaleSlug.mockImplementationOnce( () => 'de' );
expect( localizeUrl( 'https://en.support.wordpress.com/' ) ).toEqual(
'https://wordpress.com/de/support/'
);
} );
} );
describe( '#getPathParts', () => {
test( 'should split path', () => {
expect( getPathParts( '/show/me/the/money' ) ).toEqual( [
'',
'show',
'me',
'the',
'money',
] );
} );
test( 'should split path and remove trailing slash', () => {
expect( getPathParts( '/show/me/the/money/' ) ).toEqual( [
'',
'show',
'me',
'the',
'money',
] );
} );
} );
describe( 'filterLanguageRevisions()', () => {
const valid = {
en: 123,
ja: 456,
};
test( 'should leave a valid object as it is', () => {
expect( filterLanguageRevisions( valid ) ).toEqual( valid );
} );
test( 'should filter out unexpected keys.', () => {
const invalid = {
hahahaha: 999,
...valid,
};
expect( filterLanguageRevisions( invalid ) ).toEqual( valid );
} );
test( 'should filter out unexpected values.', () => {
const invalid = {
es: 'to crash or not to crash, that is the problem.',
...valid,
};
expect( filterLanguageRevisions( invalid ) ).toEqual( valid );
} );
} );
describe( 'translationExists()', function () {
it( 'should return true for a simple translation', function () {
expect( translationExists( 'test1' ) ).toBe( true );
} );
it( 'should return false for a string without translation', function () {
getWpI18nLocaleSlug.mockImplementationOnce( () => 'fr' );
expect(
translationExists(
'It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness…'
)
).toBe( false );
} );
it( 'should return true for a simple translation when using default locale', function () {
expect( translationExists( 'test1' ) ).toBe( true );
} );
it( 'should return true for a string without translation when using default locale', function () {
expect(
translationExists(
'It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness…'
)
).toBe( true );
} );
} );
describe( 'isMagnificentLocale()', function () {
it( 'should return true for magnificent locales', function () {
[
'es',
'pt-br',
'de',
'fr',
'he',
'ja',
'it',
'nl',
'ru',
'tr',
'id',
'zh-cn',
'zh-tw',
'ko',
'ar',
'sv',
].forEach( ( locale ) => {
expect( isMagnificentLocale( locale ) ).toBe( true );
} );
} );
it( 'should return false for non-magnificent locales', function () {
expect( isMagnificentLocale( 'bg' ) ).toBe( false );
expect( isMagnificentLocale( 'ro' ) ).toBe( false );
} );
it( 'should return false for english locale', function () {
expect( isMagnificentLocale( 'en' ) ).toBe( false );
} );
} );
} );
|