Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmariuzzo committed Feb 11, 2019
1 parent ca04f45 commit c826338
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
node_modules
npm-debug.log
.size-snapshot.json
cjs
esm
umd
.size-snapshot.json
lib
10 changes: 8 additions & 2 deletions src/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ const anyIntervalRegexp = /({\s*(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*)\s*})|([
export default class Lang {
locale: string = null
fallback: string = null
messages: FlattenObject = null
messages: FlattenObject = {}

constructor(options: Options) {
if (!options) {
throw new Error('options must be defined')
}

this.locale = options.locale || inferLocale() || DEFAULT_LOCALE
this.fallback = options.fallback
this.setMessages(options.messages)
}

setMessages(messages: Messages) {
this.messages = flat(messages)
if (messages) {
this.messages = flat(messages)
}
}

getLocale(): string | null {
Expand Down
14 changes: 7 additions & 7 deletions test/lang.choice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ const messages = require('./fixture/messages')
describe('The lang.choice() method', () => {
let lang

beforeEach(function() {
lang = new Lang({
messages: messages
})
beforeEach(() => {
lang = new Lang({ messages })
})

it('should return the passed key when not found', () => {
Expand Down Expand Up @@ -36,12 +34,13 @@ describe('The lang.choice() method', () => {
}
})
lang.setLocale('ru')

expect(lang.choice('plural.year', 1)).toBe('1 год')
expect(lang.choice('plural.year', 2)).toBe('2 года')
expect(lang.choice('plural.year', 5)).toBe('5 лет')
})

it('should return the expected message using math intervals', function() {
it('should return the expected message using math intervals', () => {
lang.setMessages({
'en.test': {
set: '{0} a|{1} :count b|[2,Inf] :count c',
Expand All @@ -50,6 +49,7 @@ describe('The lang.choice() method', () => {
infinityStar: '[*,-1] :count Negative|[0,*] :count Positive'
}
})

expect(lang.choice('test.set', 0)).toBe('a')
expect(lang.choice('test.set', 1)).toBe('1 b')
expect(lang.choice('test.set', 2)).toBe('2 c')
Expand Down Expand Up @@ -79,12 +79,12 @@ describe('The lang.choice() method', () => {
).toBe('The foo must be accepted.')
})

it('should return the expected message with changed locale', function() {
it('should return the expected message with changed locale', () => {
expect(lang.choice('messages.home', 1)).toBe('Home')
expect(lang.choice('messages.home', 1, {}, 'es')).toBe('Inicio')
})

it('should return the expected message using the fallback language', function() {
it('should return the expected message using the fallback language', () => {
lang.setMessages({
'en.messages': {
box: ':count box|:count boxes'
Expand Down
18 changes: 18 additions & 0 deletions test/lang.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import Lang from '../src/lang'
const messages = require('./fixture/messages')

describe('The Lang class object', () => {
it('should not throw on non options', () => {
expect(() => {
const lang = new Lang(null)
lang.get('foo')
}).toThrow()
})

it('should not throw on non messages', () => {
expect(() => {
const lang = new Lang({ messages: null })
lang.get('foo')
}).not.toThrow()
})
})

0 comments on commit c826338

Please sign in to comment.