You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
42 lines
1.1 KiB
import { describe, expect, it } from 'vitest';
|
|
import { createActiveNumbers } from '../active-numbers.svelte';
|
|
|
|
describe('createActiveNumbers()', () => {
|
|
it('syncs locale source changes and notifies active listeners', () => {
|
|
let locale = 'en-US';
|
|
let listener: ((locale: string) => void) | undefined;
|
|
const source = {
|
|
getLocale: () => locale,
|
|
onLocaleChange(fn: (nextLocale: string) => void) {
|
|
listener = fn;
|
|
return () => {
|
|
listener = undefined;
|
|
};
|
|
}
|
|
};
|
|
const nums = createActiveNumbers({ localeSource: source });
|
|
const localeChanges: string[] = [];
|
|
let preferenceChanges = 0;
|
|
|
|
nums.onLocaleChange((nextLocale) => localeChanges.push(nextLocale));
|
|
nums.onPreferenceChange(() => {
|
|
preferenceChanges++;
|
|
});
|
|
|
|
locale = 'de-DE';
|
|
listener?.(locale);
|
|
|
|
expect(nums.getLocale()).toBe('de-DE');
|
|
expect(nums.getDecimalSeparator()).toBe(',');
|
|
expect(localeChanges).toEqual(['de-DE']);
|
|
expect(preferenceChanges).toBe(1);
|
|
|
|
nums.setGrouping(false);
|
|
expect(nums.getGrouping()).toBe(false);
|
|
expect(preferenceChanges).toBe(2);
|
|
|
|
nums.dispose();
|
|
expect(listener).toBeUndefined();
|
|
});
|
|
});
|