Skip to content

Commit

Permalink
CB-5753 add more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
devnaumov committed Oct 14, 2024
1 parent 6c4cdfd commit f278cca
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
3 changes: 2 additions & 1 deletion webapp/packages/core-client-activity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"clean": "rimraf --glob dist",
"lint": "eslint ./src/ --ext .ts,.tsx",
"validate-dependencies": "core-cli-validate-dependencies",
"update-ts-references": "yarn run clean && typescript-resolve-references"
"update-ts-references": "yarn run clean && typescript-resolve-references",
"test": "core-cli-test"
},
"dependencies": {
"@cloudbeaver/core-di": "^0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';

import { ClientActivityService } from './ClientActivityService.js';
import { ClientActivityService, INACTIVE_PERIOD_TIME } from './ClientActivityService.js';

jest.useFakeTimers();

Expand Down Expand Up @@ -39,7 +39,7 @@ describe('ClientActivityService', () => {
clientActivityService.updateActivity();
expect(clientActivityService.isActive).toBe(true);

jest.advanceTimersByTime(1000 * 60);
jest.advanceTimersByTime(INACTIVE_PERIOD_TIME);

expect(clientActivityService.isActive).toBe(false);
});
Expand All @@ -48,13 +48,18 @@ describe('ClientActivityService', () => {
clientActivityService.updateActivity();
expect(setTimeout).toHaveBeenCalledTimes(1);

jest.advanceTimersByTime(Math.random() * INACTIVE_PERIOD_TIME - 1);

clientActivityService.updateActivity();
expect(clearTimeout).toHaveBeenCalledTimes(1);
expect(setTimeout).toHaveBeenCalledTimes(2);
});

it('should clear timer and reset activity when resetActivity is called', () => {
clientActivityService.updateActivity();

jest.advanceTimersByTime(Math.random() * INACTIVE_PERIOD_TIME - 1);

clientActivityService.resetActivity();

expect(clientActivityService.isActive).toBe(false);
Expand All @@ -67,7 +72,7 @@ describe('ClientActivityService', () => {
clientActivityService.updateActivity();
expect(onActiveStateChangeSpy).toHaveBeenCalledWith(true);

jest.advanceTimersByTime(1000 * 60);
jest.advanceTimersByTime(INACTIVE_PERIOD_TIME);
expect(onActiveStateChangeSpy).toHaveBeenCalledWith(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { makeObservable, observable } from 'mobx';
import { injectable } from '@cloudbeaver/core-di';
import { Executor, type IExecutor } from '@cloudbeaver/core-executor';

const INACTIVE_PERIOD_TIME = 1000 * 60;
export const INACTIVE_PERIOD_TIME = 1000 * 60;

@injectable()
export class ClientActivityService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,23 @@ describe('LocalizationService', () => {
expect(service.supportedLanguages.length).toBeGreaterThan(0);
});

it('should set supported languages and default to first supported language', () => {
it('should set supported languages', () => {
const locales: ILocale[] = [
{ isoCode: 'de', name: 'German', nativeName: 'Deutsch' },
{ isoCode: 'es', name: 'Spanish', nativeName: 'Español' },
];

service.setSupportedLanguages(locales);
expect(service.supportedLanguages.length).toBe(2);
});

it('should set default to first supported language', () => {
const locales: ILocale[] = [
{ isoCode: 'de', name: 'German', nativeName: 'Deutsch' },
{ isoCode: 'es', name: 'Spanish', nativeName: 'Español' },
];

service.setSupportedLanguages(locales);
expect(service.currentLanguage).toBe('de');
});

Expand Down Expand Up @@ -83,4 +92,27 @@ describe('LocalizationService', () => {
const translation = service.translate(token, undefined, { name: 'John' });
expect(translation).toBe('Welcome, John!');
});

it('should replace multiple args in the translation string', () => {
const token = 'greeting_message';
const translationString = 'Hello, {arg:firstName} {arg:lastName}!';
service['localeMap'].set('en', new Map([[token, translationString]]));
service.setLanguage('en');

const translation = service.translate(token, undefined, { firstName: 'John', lastName: 'Doe' });
expect(translation).toBe('Hello, John Doe!');
});

it('should return true for supported language', () => {
service.register();

expect(service.isLanguageSupported('en')).toBe(true);
expect(service.isLanguageSupported('ru')).toBe(true);
});

it('should return false for unsupported language', () => {
service.register();

expect(service.isLanguageSupported('n/a')).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class LocalizationService extends Bootstrap {
const firstLanguage = this.supportedLanguages[0];

if (!firstLanguage) {
//@TODO do not throw error in getter
throw new Error('No language is available');
}

Expand Down

0 comments on commit f278cca

Please sign in to comment.