-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cb 5753 u tests #2964
Cb 5753 u tests #2964
Changes from 4 commits
fbf27e7
88fd178
527eea9
7a47bf7
6c4cdfd
f278cca
e1f70a5
4d8c75d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals'; | ||
|
||
import { ClientActivityService } from './ClientActivityService.js'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('ClientActivityService', () => { | ||
let clientActivityService: ClientActivityService; | ||
|
||
beforeEach(() => { | ||
clientActivityService = new ClientActivityService(); | ||
|
||
jest.spyOn(global, 'setTimeout'); | ||
jest.spyOn(global, 'clearTimeout'); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllTimers(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should initialize with isActive set to false', () => { | ||
expect(clientActivityService.isActive).toBe(false); | ||
}); | ||
|
||
it('should set isActive to true when updateActivity is called', () => { | ||
clientActivityService.updateActivity(); | ||
expect(clientActivityService.isActive).toBe(true); | ||
}); | ||
|
||
it('should reset activity after the timeout period', () => { | ||
clientActivityService.updateActivity(); | ||
expect(clientActivityService.isActive).toBe(true); | ||
|
||
jest.advanceTimersByTime(1000 * 60); | ||
|
||
expect(clientActivityService.isActive).toBe(false); | ||
}); | ||
|
||
it('should clear previous timer if updateActivity is called multiple times', () => { | ||
clientActivityService.updateActivity(); | ||
expect(setTimeout).toHaveBeenCalledTimes(1); | ||
|
||
clientActivityService.updateActivity(); | ||
expect(clearTimeout).toHaveBeenCalledTimes(1); | ||
expect(setTimeout).toHaveBeenCalledTimes(2); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would be nice to emulate running time here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can randomly tick in range from |
||
}); | ||
|
||
it('should clear timer and reset activity when resetActivity is called', () => { | ||
clientActivityService.updateActivity(); | ||
clientActivityService.resetActivity(); | ||
|
||
expect(clientActivityService.isActive).toBe(false); | ||
expect(clearTimeout).toHaveBeenCalled(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here for running time. I suggest to emulate cases like it is actually happening |
||
}); | ||
|
||
it('should call onActiveStateChange executor with correct value', () => { | ||
const onActiveStateChangeSpy = jest.spyOn(clientActivityService.onActiveStateChange, 'execute'); | ||
|
||
clientActivityService.updateActivity(); | ||
expect(onActiveStateChangeSpy).toHaveBeenCalledWith(true); | ||
|
||
jest.advanceTimersByTime(1000 * 60); | ||
expect(onActiveStateChangeSpy).toHaveBeenCalledWith(false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dont forget to mock Executor also so the test is more moduled. cause if something goes wrong in Executor logic, this test is gonna be fallen as well |
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { beforeEach, describe, expect, it } from '@jest/globals'; | ||
|
||
import { DEFAULT_LOCALE } from './DEFAULT_LOCALE.js'; | ||
import { type ILocale } from './ILocale.js'; | ||
import { LocalizationService } from './LocalizationService.js'; | ||
|
||
describe('LocalizationService', () => { | ||
let service: LocalizationService; | ||
|
||
beforeEach(() => { | ||
service = new LocalizationService(); | ||
}); | ||
|
||
it('should initialize with default locale', () => { | ||
service.register(); | ||
expect(service.currentLanguage).toBe(DEFAULT_LOCALE.isoCode); | ||
expect(service.supportedLanguages.length).toBeGreaterThan(0); | ||
sergeyteleshev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
it('should set supported languages and 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.supportedLanguages.length).toBe(2); | ||
expect(service.currentLanguage).toBe('de'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please, split this to 2 different tests:
|
||
|
||
it('should return default locale if the current language is not supported', () => { | ||
service.setLanguage('es'); | ||
expect(service.currentLanguage).toBe(DEFAULT_LOCALE.isoCode); | ||
}); | ||
|
||
it('should throw error if there are no supported languages', () => { | ||
service.supportedLanguages = []; | ||
expect(() => service.currentLanguage).toThrowError('No language is available'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems weird that getter can throw an error. I suggest to add TODO to refactor this logic somewhen in the |
||
}); | ||
|
||
it('should change the current language', async () => { | ||
service.register(); | ||
await service.changeLocale('ru'); | ||
expect(service.currentLanguage).toBe('ru'); | ||
}); | ||
|
||
it('should throw an error when changing to an unsupported language', async () => { | ||
await expect(service.changeLocale('jp')).rejects.toThrowError("Language 'jp' is not supported"); | ||
}); | ||
|
||
it('should translate a token with a fallback', () => { | ||
const token = 'greeting'; | ||
const fallback = 'Hello, World!'; | ||
|
||
service.changeLocale('en'); | ||
service['localeMap'].set('en', new Map([[token, 'Hello']])); | ||
|
||
const translation = service.translate(token, fallback); | ||
expect(translation).toBe('Hello'); | ||
}); | ||
|
||
it('should return fallback when translation is missing', () => { | ||
const token = 'nonexistent_token'; | ||
const fallback = 'Fallback'; | ||
|
||
const translation = service.translate(token, fallback); | ||
expect(translation).toBe(fallback); | ||
}); | ||
|
||
it('should replace args in the translation string', () => { | ||
const token = 'welcome_message'; | ||
const translationString = 'Welcome, {arg:name}!'; | ||
service['localeMap'].set('en', new Map([[token, translationString]])); | ||
service.setLanguage('en'); | ||
|
||
const translation = service.translate(token, undefined, { name: 'John' }); | ||
expect(translation).toBe('Welcome, John!'); | ||
}); | ||
sergeyteleshev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
sergeyteleshev marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { NodeManagerUtils } from './NodeManagerUtils.js'; | ||
|
||
describe('NodeManagerUtils', () => { | ||
describe('connectionIdToConnectionNodeId', () => { | ||
it('should prepend "database://" to the connectionId', () => { | ||
const connectionId = '12345'; | ||
const result = NodeManagerUtils.connectionIdToConnectionNodeId(connectionId); | ||
expect(result).toBe('database://12345'); | ||
}); | ||
|
||
it('should work with different connectionId values', () => { | ||
expect(NodeManagerUtils.connectionIdToConnectionNodeId('abc')).toBe('database://abc'); | ||
expect(NodeManagerUtils.connectionIdToConnectionNodeId('')).toBe('database://'); | ||
}); | ||
}); | ||
|
||
describe('isDatabaseObject', () => { | ||
it('should return true for objectIds starting with "database://"', () => { | ||
expect(NodeManagerUtils.isDatabaseObject('database://123')).toBe(true); | ||
expect(NodeManagerUtils.isDatabaseObject('database://abc')).toBe(true); | ||
}); | ||
|
||
it('should return false for objectIds not starting with "database://"', () => { | ||
expect(NodeManagerUtils.isDatabaseObject('http://example.com')).toBe(false); | ||
expect(NodeManagerUtils.isDatabaseObject('12345')).toBe(false); | ||
expect(NodeManagerUtils.isDatabaseObject('')).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('concatSchemaAndCatalog', () => { | ||
it('should concatenate schemaId and catalogId with "@" when both are provided', () => { | ||
const result = NodeManagerUtils.concatSchemaAndCatalog('schema1', 'catalog1'); | ||
expect(result).toBe('schema1@catalog1'); | ||
}); | ||
|
||
it('should return just schemaId if catalogId is undefined', () => { | ||
expect(NodeManagerUtils.concatSchemaAndCatalog(undefined, 'schema1')).toBe('schema1'); | ||
}); | ||
|
||
it('should return just catalogId if schemaId is undefined', () => { | ||
expect(NodeManagerUtils.concatSchemaAndCatalog('catalog1', undefined)).toBe('catalog1'); | ||
}); | ||
|
||
it('should return an empty string if both are undefined', () => { | ||
expect(NodeManagerUtils.concatSchemaAndCatalog(undefined, undefined)).toBe(''); | ||
}); | ||
|
||
it('should handle cases with empty strings', () => { | ||
expect(NodeManagerUtils.concatSchemaAndCatalog('', 'catalog1')).toBe('catalog1'); | ||
expect(NodeManagerUtils.concatSchemaAndCatalog('schema1', '')).toBe('schema1'); | ||
expect(NodeManagerUtils.concatSchemaAndCatalog('', '')).toBe(''); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* CloudBeaver - Cloud Database Manager | ||
* Copyright (C) 2020-2024 DBeaver Corp and others | ||
* | ||
* Licensed under the Apache License, Version 2.0. | ||
* you may not use this file except in compliance with the License. | ||
*/ | ||
import { describe, expect, it } from '@jest/globals'; | ||
|
||
import { getDomainFromUrl } from './getDomainFromUrl.js'; | ||
|
||
describe('getDomainFromUrl', () => { | ||
it('should return domain for a valid URL with http protocol', () => { | ||
const url = 'http://example.com/path'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe('example.com'); | ||
}); | ||
|
||
it('should return domain for a valid URL with https protocol', () => { | ||
const url = 'https://example.com/path'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe('example.com'); | ||
}); | ||
|
||
it('should return domain for a valid URL with www', () => { | ||
const url = 'https://www.example.com/path'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe('www.example.com'); | ||
}); | ||
|
||
it('should return domain for a URL with a subdomain', () => { | ||
const url = 'https://blog.example.com'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe('blog.example.com'); | ||
}); | ||
|
||
it('should return empty string for an invalid URL', () => { | ||
const url = 'not-a-valid-url'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe(''); | ||
}); | ||
|
||
it('should return empty string for an empty string input', () => { | ||
const url = ''; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe(''); | ||
}); | ||
|
||
it('should return domain for a URL with query parameters', () => { | ||
const url = 'https://example.com/search?q=test'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe('example.com'); | ||
}); | ||
|
||
it('should return domain for a URL with port number', () => { | ||
const url = 'https://example.com:8080/path'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe('example.com'); | ||
}); | ||
|
||
it('should return domain for an IP address URL', () => { | ||
const url = 'http://127.0.0.1:3000'; | ||
const domain = getDomainFromUrl(url); | ||
expect(domain).toBe('127.0.0.1'); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is fine to use
INACTIVE_PERIOD_TIME
const here