From 21afa3d6b1cd11a663b75a24f9de9514ff93a48b Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Tue, 26 Nov 2019 13:18:07 -0300 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20tests=20for=20getC?= =?UTF-8?q?lientLocale=20precedence?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/includes/utils.ts | 2 + test/client/utils.test.ts | 78 +++++++++++++++++++++++++++++++++--- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/src/client/includes/utils.ts b/src/client/includes/utils.ts index 0594919..4dae6a5 100644 --- a/src/client/includes/utils.ts +++ b/src/client/includes/utils.ts @@ -27,7 +27,9 @@ const getFromQueryString = (queryString: string, key: string) => { const getFirstMatch = (base: string, pattern: RegExp) => { const match = pattern.exec(base) + // istanbul ignore if if (!match) return null + // istanbul ignore else return match[1] || null } diff --git a/test/client/utils.test.ts b/test/client/utils.test.ts index 76a87c6..43e385e 100644 --- a/test/client/utils.test.ts +++ b/test/client/utils.test.ts @@ -19,30 +19,96 @@ describe('getting client locale', () => { test('gets the locale based on the passed hash parameter', () => { window.location.hash = '#locale=en-US&lang=pt-BR' - expect(getClientLocale({ hash: 'lang' })).toBe('pt-BR') + expect( + getClientLocale({ + hash: 'lang', + }) + ).toBe('pt-BR') }) test('gets the locale based on the passed search parameter', () => { window.location.search = '?locale=en-US&lang=pt-BR' - expect(getClientLocale({ search: 'lang' })).toBe('pt-BR') + expect( + getClientLocale({ + search: 'lang', + }) + ).toBe('pt-BR') }) test('gets the locale based on the navigator language', () => { - expect(getClientLocale({ navigator: true })).toBe(window.navigator.language) + expect( + getClientLocale({ + navigator: true, + }) + ).toBe(window.navigator.language) }) test('gets the locale based on the pathname', () => { window.location.pathname = '/en-US/foo/' - expect(getClientLocale({ pathname: /^\/(.*?)\// })).toBe('en-US') + expect( + getClientLocale({ + pathname: /^\/(.*?)\//, + }) + ).toBe('en-US') }) test('gets the locale base on the hostname', () => { window.location.hostname = 'pt.example.com' - expect(getClientLocale({ hostname: /^(.*?)\./ })).toBe('pt') + expect( + getClientLocale({ + hostname: /^(.*?)\./, + }) + ).toBe('pt') + }) + + test('hostname precedes pathname', () => { + window.location.pathname = '/en-US/foo/' + window.location.hostname = 'pt.example.com' + expect( + getClientLocale({ + hostname: /^(.*?)\./, + pathname: /^\/(.*?)\//, + }) + ).toBe('pt') + }) + + test('pathname precedes navigator', () => { + window.location.pathname = '/it-IT/foo/' + expect( + getClientLocale({ + pathname: /^\/(.*?)\//, + navigator: true, + }) + ).toBe('it-IT') + }) + + test('navigator precedes search', () => { + window.location.search = '?lang=pt-BR' + expect( + getClientLocale({ + navigator: true, + search: 'lang', + }) + ).toBe('en-US') + }) + + test('search precedes hash', () => { + window.location.hash = '#lang=pt-BR' + window.location.search = '?lang=it-IT' + expect( + getClientLocale({ + hash: 'lang', + search: 'lang', + }) + ).toBe('it-IT') }) test('returns null if no locale was found', () => { - expect(getClientLocale({ search: 'lang' })).toBe(null) + expect( + getClientLocale({ + search: 'lang', + }) + ).toBe(null) }) })