|
|
@ -1,7 +1,7 @@ |
|
|
import { Router } from '@angular/router'; |
|
|
import { Router } from '@angular/router'; |
|
|
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; |
|
|
import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; |
|
|
import { Store, Actions } from '@ngxs/store'; |
|
|
import { Actions, Store } from '@ngxs/store'; |
|
|
import { Observable, of, Subject } from 'rxjs'; |
|
|
import { of, Subject } from 'rxjs'; |
|
|
import { LocalizationService } from '../services/localization.service'; |
|
|
import { LocalizationService } from '../services/localization.service'; |
|
|
|
|
|
|
|
|
describe('LocalizationService', () => { |
|
|
describe('LocalizationService', () => { |
|
|
@ -75,4 +75,172 @@ describe('LocalizationService', () => { |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#localize', () => { |
|
|
|
|
|
test.each` |
|
|
|
|
|
resource | key | defaultValue | expected |
|
|
|
|
|
${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} |
|
|
|
|
|
${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'foo'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'x'} | ${'y'} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${'a'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'foo'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'x'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'a'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'foo'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'x'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'a'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
`(
|
|
|
|
|
|
'should return observable $expected when resource name is $resource and key is $key', |
|
|
|
|
|
async ({ resource, key, defaultValue, expected }) => { |
|
|
|
|
|
store.select.andReturn( |
|
|
|
|
|
of({ |
|
|
|
|
|
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
|
|
|
|
|
defaultResourceName: 'x', |
|
|
|
|
|
}), |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const result = await service.localize(resource, key, defaultValue).toPromise(); |
|
|
|
|
|
|
|
|
|
|
|
expect(result).toBe(expected); |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#localizeSync', () => { |
|
|
|
|
|
test.each` |
|
|
|
|
|
resource | key | defaultValue | expected |
|
|
|
|
|
${'foo'} | ${'bar'} | ${'DEFAULT'} | ${'baz'} |
|
|
|
|
|
${'x'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'a'} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${'bar'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'foo'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'x'} | ${'y'} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${'a'} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${'y'} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'foo'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'x'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'a'} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${''} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'foo'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'x'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${'a'} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${''} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${undefined} | ${undefined} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
`(
|
|
|
|
|
|
'should return $expected when resource name is $resource and key is $key', |
|
|
|
|
|
({ resource, key, defaultValue, expected }) => { |
|
|
|
|
|
store.selectSnapshot.andReturn({ |
|
|
|
|
|
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
|
|
|
|
|
defaultResourceName: 'x', |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const result = service.localizeSync(resource, key, defaultValue); |
|
|
|
|
|
|
|
|
|
|
|
expect(result).toBe(expected); |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#localizeWithFallback', () => { |
|
|
|
|
|
test.each` |
|
|
|
|
|
resources | keys | defaultValue | expected |
|
|
|
|
|
${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} |
|
|
|
|
|
${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} |
|
|
|
|
|
${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
`(
|
|
|
|
|
|
'should return observable $expected when resource names are $resources and keys are $keys', |
|
|
|
|
|
async ({ resources, keys, defaultValue, expected }) => { |
|
|
|
|
|
store.select.andReturn( |
|
|
|
|
|
of({ |
|
|
|
|
|
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
|
|
|
|
|
defaultResourceName: 'x', |
|
|
|
|
|
}), |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
const result = await service |
|
|
|
|
|
.localizeWithFallback(resources, keys, defaultValue) |
|
|
|
|
|
.toPromise(); |
|
|
|
|
|
|
|
|
|
|
|
expect(result).toBe(expected); |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
describe('#localizeWithFallbackSync', () => { |
|
|
|
|
|
test.each` |
|
|
|
|
|
resources | keys | defaultValue | expected |
|
|
|
|
|
${['foo']} | ${['bar']} | ${'DEFAULT'} | ${'baz'} |
|
|
|
|
|
${['x']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['']} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${[]} | ${['bar']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['foo']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['x']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['']} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${[]} | ${['y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['foo']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'baz'} |
|
|
|
|
|
${['x']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['']} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${[]} | ${['bar', 'y']} | ${'DEFAULT'} | ${'z'} |
|
|
|
|
|
${['foo']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['x']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['']} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${[]} | ${['']} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['foo']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['x']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['a', 'b', 'c']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${['']} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
${[]} | ${[]} | ${'DEFAULT'} | ${'DEFAULT'} |
|
|
|
|
|
`(
|
|
|
|
|
|
'should return $expected when resource names are $resources and keys are $keys', |
|
|
|
|
|
({ resources, keys, defaultValue, expected }) => { |
|
|
|
|
|
store.selectSnapshot.andReturn({ |
|
|
|
|
|
values: { foo: { bar: 'baz' }, x: { y: 'z' } }, |
|
|
|
|
|
defaultResourceName: 'x', |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
const result = service.localizeWithFallbackSync(resources, keys, defaultValue); |
|
|
|
|
|
|
|
|
|
|
|
expect(result).toBe(expected); |
|
|
|
|
|
}, |
|
|
|
|
|
); |
|
|
|
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|