From 7dee09b6681c2013dacfa27b5327d835d15ded62 Mon Sep 17 00:00:00 2001 From: Christian Kaisermann Date: Thu, 28 Nov 2019 15:00:04 -0300 Subject: [PATCH] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20tests=20for=20form?= =?UTF-8?q?at=20store?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../formatters.test.ts} | 14 +-- test/client/index.test.ts | 6 ++ test/client/stores/format.test.ts | 92 +++++++++++++++++++ test/fixtures/en.json | 13 +-- test/fixtures/es.json | 11 +-- test/fixtures/pt.json | 10 +- 6 files changed, 117 insertions(+), 29 deletions(-) rename test/client/{formatter.test.ts => includes/formatters.test.ts} (90%) create mode 100644 test/client/index.test.ts create mode 100644 test/client/stores/format.test.ts diff --git a/test/client/formatter.test.ts b/test/client/includes/formatters.test.ts similarity index 90% rename from test/client/formatter.test.ts rename to test/client/includes/formatters.test.ts index 426d350..17d5723 100644 --- a/test/client/formatter.test.ts +++ b/test/client/includes/formatters.test.ts @@ -3,8 +3,8 @@ import { getDateFormatter, getTimeFormatter, getMessageFormatter, -} from '../../src/client/includes/formatters' -import { init } from '../../src/client/configs' +} from '../../../src/client/includes/formatters' +import { init } from '../../../src/client/configs' beforeEach(() => { init({ fallbackLocale: undefined }) @@ -27,7 +27,7 @@ describe('number formatter', () => { test('should format a number with a custom format', () => { init({ fallbackLocale: 'en', - formats: require('../fixtures/formats.json'), + formats: require('../../fixtures/formats.json'), }) expect(getNumberFormatter({ format: 'brl' }).format(number)).toBe( @@ -59,7 +59,7 @@ describe('date formatter', () => { test('should throw if passed a non-existing format', () => { init({ fallbackLocale: 'en', - formats: require('../fixtures/formats.json'), + formats: require('../../fixtures/formats.json'), }) expect(() => @@ -70,7 +70,7 @@ describe('date formatter', () => { test('should format a date with a custom format', () => { init({ fallbackLocale: 'en', - formats: require('../fixtures/formats.json'), + formats: require('../../fixtures/formats.json'), }) expect(getDateFormatter({ format: 'customDate' }).format(date)).toBe( @@ -102,7 +102,7 @@ describe('time formatter', () => { test('should format a time with a custom format', () => { init({ fallbackLocale: 'en', - formats: require('../fixtures/formats.json'), + formats: require('../../fixtures/formats.json'), }) expect(getTimeFormatter({ format: 'customTime' }).format(time)).toBe( @@ -113,7 +113,7 @@ describe('time formatter', () => { test('should throw if passed a non-existing format', () => { init({ fallbackLocale: 'en', - formats: require('../fixtures/formats.json'), + formats: require('../../fixtures/formats.json'), }) expect(() => diff --git a/test/client/index.test.ts b/test/client/index.test.ts new file mode 100644 index 0000000..1867b05 --- /dev/null +++ b/test/client/index.test.ts @@ -0,0 +1,6 @@ +import { defineMessages } from '../../src/client' + +test('defineMessages returns the identity of its first argument', () => { + const obj = {} + expect(obj).toBe(defineMessages(obj)) +}) diff --git a/test/client/stores/format.test.ts b/test/client/stores/format.test.ts new file mode 100644 index 0000000..7d99eb7 --- /dev/null +++ b/test/client/stores/format.test.ts @@ -0,0 +1,92 @@ +import { Formatter } from '../../../src/client/types/index' +import { $format } from '../../../src/client/stores/format' +import { init } from '../../../src/client/configs' +import { addMessages } from '../../../src/client/stores/dictionary' +import { $locale } from '../../../src/client/stores/locale' + +let format: Formatter +$format.subscribe(f => (format = f)) + +addMessages('en', require('../../fixtures/en.json')) +addMessages('en-GB', require('../../fixtures/en-GB.json')) +addMessages('pt', require('../../fixtures/pt.json')) +addMessages('pt-BR', require('../../fixtures/pt-BR.json')) +addMessages('pt-PT', require('../../fixtures/pt-PT.json')) + +beforeEach(() => { + init({ fallbackLocale: 'en' }) +}) + +test('formats a message by its id and the current locale', () => { + expect(format({ id: 'form.field_1_name' })).toBe('Name') +}) + +test('formats a message by its id and the a passed locale', () => { + expect(format({ id: 'form.field_1_name', locale: 'pt' })).toBe('Nome') +}) + +test('formats a message with interpolated values', () => { + expect(format({ id: 'photos', values: { n: 0 } })).toBe('You have no photos.') + expect(format({ id: 'photos', values: { n: 1 } })).toBe('You have one photo.') + expect(format({ id: 'photos', values: { n: 21 } })).toBe( + 'You have 21 photos.' + ) +}) + +test('accepts a message id as first argument', () => { + expect(format('form.field_1_name')).toBe('Name') +}) + +test('accepts a message id as first argument and formatting options as second', () => { + expect(format('form.field_1_name', { locale: 'pt' })).toBe('Nome') +}) + +test('throws if no locale is set', () => { + $locale.set(null) + expect(() => format('form.field_1_name')).toThrow( + '[svelte-i18n] Cannot format a message without first setting the initial locale.' + ) +}) + +test('uses a missing message default value', () => { + expect(format('missing', { default: 'Missing Default' })).toBe( + 'Missing Default' + ) +}) + +test('warn on missing messages', () => { + const warn = global.console.warn + global.console.warn = jest.fn() + + format('missing') + + expect(console.warn).toBeCalledWith( + `[svelte-i18n] The message "missing" was not found in "en".` + ) + + global.console.warn = warn +}) + +describe('format utilities', () => { + test('time', () => { + expect(format.time(new Date(2019, 0, 1, 20, 37))).toBe('8:37 PM') + }) + test('date', () => { + expect(format.date(new Date(2019, 0, 1, 20, 37))).toBe('1/1/19') + }) + test('number', () => { + expect(format.number(123123123)).toBe('123,123,123') + }) + test('capital', () => { + expect(format.capital('title')).toBe('Page title') + }) + test('title', () => { + expect(format.title('title')).toBe('Page Title') + }) + test('upper', () => { + expect(format.upper('title')).toBe('PAGE TITLE') + }) + test('lower', () => { + expect(format.lower('title')).toBe('page title') + }) +}) diff --git a/test/fixtures/en.json b/test/fixtures/en.json index 432f9d7..62186de 100644 --- a/test/fixtures/en.json +++ b/test/fixtures/en.json @@ -1,14 +1,9 @@ { - "hi": "hi yo", - "switch.lang": "Switch language", - "page": { - "title_home": "Home" - }, - "greeting": { - "ask": "Please type your name", - "message": "Hello {name}, how are you?" + "form": { + "field_1_name": "Name", + "field_2_name": "Lastname" }, "photos": "You have {n, plural, =0 {no photos.} =1 {one photo.} other {# photos.}}", - "cats": "I have {n, number} {n,plural,one{cat}other{cats}}", + "title": "Page title", "sneakers": "sneakers" } diff --git a/test/fixtures/es.json b/test/fixtures/es.json index 9396956..6436bcd 100644 --- a/test/fixtures/es.json +++ b/test/fixtures/es.json @@ -1,11 +1,8 @@ { - "hi": "hola yo", - "switch.lang": "Cambiar de idioma", - "greeting": { - "ask": "Por favor escriba su nombre", - "message": "Hola {name}, cómo estás?" + "form": { + "field_1_name": "Nombre", + "field_2_name": "Apellido" }, - "photos": "Tienes {n, plural, =0 {0 fotos.} =1 {una foto.} other {# fotos.}}", - "cats": "Yo tengo {n, number} {n,plural,one{gato}other{gatos}}", + "title": "Título de la página", "sneakers": "zapatillas" } diff --git a/test/fixtures/pt.json b/test/fixtures/pt.json index cfa7561..5c554ee 100644 --- a/test/fixtures/pt.json +++ b/test/fixtures/pt.json @@ -1,11 +1,9 @@ { - "hi": "olá você", - "switch.lang": "Trocar idioma", - "greeting": { - "ask": "Por favor, digite seu nome", - "message": "Olá {name}, como vai?" + "form": { + "field_1_name": "Nome", + "field_2_name": "Sobrenome" }, "photos": "Você {n, plural, =0 {não tem fotos.} =1 {tem uma foto.} other {tem # fotos.}}", - "cats": "Tenho {n, number} {n,plural,=0{gatos}one{gato}other{gatos}}", + "title": "Título da página", "sneakers": "tênis" }