diff options
Diffstat (limited to 'server/sonar-ui-common/helpers/__tests__')
14 files changed, 324 insertions, 27 deletions
diff --git a/server/sonar-ui-common/helpers/__tests__/colors-test.ts b/server/sonar-ui-common/helpers/__tests__/colors-test.ts index ccc58d305b6..ae80a29e093 100644 --- a/server/sonar-ui-common/helpers/__tests__/colors-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/colors-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/dates-test.ts b/server/sonar-ui-common/helpers/__tests__/dates-test.ts index 688fca9904c..9482f6d6476 100644 --- a/server/sonar-ui-common/helpers/__tests__/dates-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/dates-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/handleRequiredAuthentication-test.ts b/server/sonar-ui-common/helpers/__tests__/handleRequiredAuthentication-test.ts index fa90439d195..abb391a2e21 100644 --- a/server/sonar-ui-common/helpers/__tests__/handleRequiredAuthentication-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/handleRequiredAuthentication-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/init-test.ts b/server/sonar-ui-common/helpers/__tests__/init-test.ts index 44d2dbe984a..8f7e927a867 100644 --- a/server/sonar-ui-common/helpers/__tests__/init-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/init-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/l10n-test.ts b/server/sonar-ui-common/helpers/__tests__/l10n-test.ts index 3252baf524e..523ac99b30b 100644 --- a/server/sonar-ui-common/helpers/__tests__/l10n-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/l10n-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or @@ -19,7 +19,17 @@ */ /* eslint-disable camelcase */ import Initializer, { getMessages } from '../init'; -import { hasMessage, translate, translateWithParameters } from '../l10n'; +import { + getLocalizedCategoryMetricName, + getLocalizedMetricDomain, + getLocalizedMetricName, + getShortMonthName, + getShortWeekDayName, + getWeekDayName, + hasMessage, + translate, + translateWithParameters, +} from '../l10n'; const originalMessages = getMessages(); const MSG = 'my_message'; @@ -96,3 +106,78 @@ describe('hasMessage', () => { expect(hasMessage('foo', 'bar')).toBe(false); }); }); + +describe('getLocalizedMetricName', () => { + const metric = { key: 'new_code', name: 'new_code_metric_name' }; + + it('should return the metric name translation', () => { + Initializer.setMessages({ 'metric.new_code.name': 'metric.new_code.name_t' }); + expect(getLocalizedMetricName(metric)).toBe('metric.new_code.name_t'); + }); + + it('should return the metric short name', () => { + Initializer.setMessages({ 'metric.new_code.short_name': 'metric.new_code.short_name_t' }); + expect(getLocalizedMetricName(metric, true)).toBe('metric.new_code.short_name_t'); + }); + + it('should fallback on name if short name is absent', () => { + Initializer.setMessages({ 'metric.new_code.name': 'metric.new_code.name_t' }); + expect(getLocalizedMetricName(metric, true)).toBe('metric.new_code.name_t'); + }); + + it('should fallback on metric name if translation is absent', () => { + expect(getLocalizedMetricName(metric)).toBe('new_code_metric_name'); + }); + + it('should fallback on metric key if nothing else is available', () => { + expect(getLocalizedMetricName({ key: 'new_code' })).toBe('new_code'); + }); +}); + +describe('getLocalizedCategoryMetricName', () => { + it('should return metric category name translation', () => { + Initializer.setMessages({ + 'metric.new_code.extra_short_name': 'metric.new_code.extra_short_name_t', + }); + expect(getLocalizedCategoryMetricName({ key: 'new_code' })).toBe( + 'metric.new_code.extra_short_name_t' + ); + }); + + it('should fallback on metric name if extra_short_name is absent', () => { + Initializer.setMessages({ 'metric.new_code.name': 'metric.new_code.name_t' }); + expect(getLocalizedCategoryMetricName({ key: 'new_code' })).toBe('metric.new_code.name_t'); + }); +}); + +describe('getLocalizedMetricDomain', () => { + it('should return metric domain name translation', () => { + Initializer.setMessages({ 'metric_domain.domain': 'metric_domain.domain_t' }); + expect(getLocalizedMetricDomain('domain')).toBe('metric_domain.domain_t'); + }); + + it('should fallback on metric domain name', () => { + expect(getLocalizedMetricDomain('domain')).toBe('domain'); + }); +}); + +describe('getShortMonthName', () => { + it('should properly translation months', () => { + Initializer.setMessages({ Jan: 'Jan_t' }); + expect(getShortMonthName(0)).toBe('Jan_t'); + }); +}); + +describe('getWeekDayName', () => { + it('should properly translation weekday', () => { + Initializer.setMessages({ Sunday: 'Sunday_t' }); + expect(getWeekDayName(0)).toBe('Sunday_t'); + }); +}); + +describe('getShortWeekDayName', () => { + it('should properly translation short weekday', () => { + Initializer.setMessages({ Sun: 'Sun_t' }); + expect(getShortWeekDayName(0)).toBe('Sun_t'); + }); +}); diff --git a/server/sonar-ui-common/helpers/__tests__/measures-test.ts b/server/sonar-ui-common/helpers/__tests__/measures-test.ts index 346f0dda3be..f9cb4ab04ce 100644 --- a/server/sonar-ui-common/helpers/__tests__/measures-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/measures-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/pages-test.ts b/server/sonar-ui-common/helpers/__tests__/pages-test.ts new file mode 100644 index 00000000000..9f4ba96c172 --- /dev/null +++ b/server/sonar-ui-common/helpers/__tests__/pages-test.ts @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { + addNoFooterPageClass, + addSideBarClass, + addWhitePageClass, + removeNoFooterPageClass, + removeSideBarClass, + removeWhitePageClass, +} from '../pages'; + +describe('class adders', () => { + it.each([ + [addSideBarClass, 'sidebar-page'], + [addNoFooterPageClass, 'no-footer-page'], + [addWhitePageClass, 'white-page'], + ])('%s should add the class', (fct, cls) => { + const toggle = jest.spyOn(document.body.classList, 'toggle'); + fct(); + expect(toggle).toBeCalledWith(cls, true); + }); +}); + +describe('class removers', () => { + it.each([ + [removeSideBarClass, 'sidebar-page'], + [removeNoFooterPageClass, 'no-footer-page'], + [removeWhitePageClass, 'white-page'], + ])('%s should add the class', (fct, cls) => { + const toggle = jest.spyOn(document.body.classList, 'toggle'); + fct(); + expect(toggle).toBeCalledWith(cls, false); + }); +}); diff --git a/server/sonar-ui-common/helpers/__tests__/path-test.ts b/server/sonar-ui-common/helpers/__tests__/path-test.ts index 997d23e055f..12f32ab7d0c 100644 --- a/server/sonar-ui-common/helpers/__tests__/path-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/path-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or @@ -17,7 +17,14 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -import { collapsedDirFromPath, cutLongWords, fileFromPath } from '../path'; +import { + collapsedDirFromPath, + collapsePath, + cutLongWords, + fileFromPath, + limitComponentName, + splitPath, +} from '../path'; describe('#collapsedDirFromPath()', () => { it('should return null when pass null', () => { @@ -78,3 +85,44 @@ describe('#cutLongWords', () => { expect(cutLongWords('This is a test')).toBe('This is a test'); }); }); + +describe('collapsePath', () => { + it('should fail fast if path is not a string', () => { + expect(collapsePath({} as string)).toBe(''); + }); + + it('should not collapse short path', () => { + const path = 'my/path'; + expect(collapsePath(path)).toBe(path); + }); + + it('should collapse path longer than the limit', () => { + const path = 'my/long/path/very/long/path'; + expect(collapsePath(path, 10)).toBe('my/.../very/long/path'); + expect(collapsePath(path, 5)).toBe('my/.../long/path'); + expect(collapsePath(path, 2)).toBe('my/.../path'); + }); +}); + +describe('splitPath', () => { + it('should split path properly', () => { + expect(splitPath('my/super/path')).toEqual({ head: 'my/super', tail: 'path' }); + expect(splitPath('my/super/very/long/path')).toEqual({ + head: 'my/super/very/long', + tail: 'path', + }); + }); +}); + +describe('limitComponentName', () => { + const name = 'my/super/name'; + + it('should fail fast if component name is not a string', () => { + expect(limitComponentName({} as string)).toBe(''); + }); + + it('should limiit component name longer than the limit', () => { + expect(limitComponentName(name)).toBe(name); + expect(limitComponentName(name, 10)).toBe('my/super/n...'); + }); +}); diff --git a/server/sonar-ui-common/helpers/__tests__/query-test.ts b/server/sonar-ui-common/helpers/__tests__/query-test.ts index 1adea91e969..4a6a671005c 100644 --- a/server/sonar-ui-common/helpers/__tests__/query-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/query-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/ratings-test.ts b/server/sonar-ui-common/helpers/__tests__/ratings-test.ts new file mode 100644 index 00000000000..619ef1cdbd7 --- /dev/null +++ b/server/sonar-ui-common/helpers/__tests__/ratings-test.ts @@ -0,0 +1,114 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import { + getCoverageRatingAverageValue, + getCoverageRatingLabel, + getDuplicationsRatingAverageValue, + getDuplicationsRatingLabel, + getSizeRatingAverageValue, + getSizeRatingLabel, +} from '../ratings'; + +describe('getCoverageRatingLabel', () => { + it('should fail', () => { + expect(() => { + getCoverageRatingLabel(-1); + }).toThrow(); + }); + it.each([ + [1, '≥ 80%'], + [2, '70% - 80%'], + [3, '50% - 70%'], + [4, '30% - 50%'], + [5, '< 30%'], + ])('should return the correct label for %s', (rating, label) => { + expect(getCoverageRatingLabel(rating)).toBe(label); + }); +}); + +describe('getCoverageRatingAverageValue', () => { + it.each([ + [1, 90], + [2, 75], + [3, 60], + [4, 40], + [5, 15], + ])('should return the correct value', (rating, value) => { + expect(getCoverageRatingAverageValue(rating)).toBe(value); + }); +}); + +describe('getDuplicationsRatingLabel', () => { + it('should fail', () => { + expect(() => { + getCoverageRatingLabel(-1); + }).toThrow(); + }); + it.each([ + [1, '< 3%'], + [2, '3% - 5%'], + [3, '5% - 10%'], + [4, '10% - 20%'], + [5, '> 20%'], + ])('should return the correct label for %s', (rating, label) => { + expect(getDuplicationsRatingLabel(rating)).toBe(label); + }); +}); + +describe('getDuplicationsRatingAverageValue', () => { + it.each([ + [1, 1.5], + [2, 4], + [3, 7.5], + [4, 15], + [5, 30], + ])('should return the correct value', (rating, value) => { + expect(getDuplicationsRatingAverageValue(rating)).toBe(value); + }); +}); + +describe('getSizeRatingLabel', () => { + it('should fail', () => { + expect(() => { + getCoverageRatingLabel(-1); + }).toThrow(); + }); + it.each([ + [1, '< 1k'], + [2, '1k - 10k'], + [3, '10k - 100k'], + [4, '100k - 500k'], + [5, '> 500k'], + ])('should return the correct label for %s', (rating, label) => { + expect(getSizeRatingLabel(rating)).toBe(label); + }); +}); + +describe('getSizeRatingAverageValue', () => { + it.each([ + [1, 500], + [2, 5000], + [3, 50000], + [4, 250000], + [5, 750000], + ])('should return the correct value', (rating, value) => { + expect(getSizeRatingAverageValue(rating)).toBe(value); + }); +}); diff --git a/server/sonar-ui-common/helpers/__tests__/request-test.ts b/server/sonar-ui-common/helpers/__tests__/request-test.ts index aee57700784..a6ffa480d2d 100644 --- a/server/sonar-ui-common/helpers/__tests__/request-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/request-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or @@ -17,7 +17,6 @@ * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ - import handleRequiredAuthentication from '../handleRequiredAuthentication'; import { checkStatus, diff --git a/server/sonar-ui-common/helpers/__tests__/scrolling-test.ts b/server/sonar-ui-common/helpers/__tests__/scrolling-test.ts index ef9f3802d2a..43ec5664178 100644 --- a/server/sonar-ui-common/helpers/__tests__/scrolling-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/scrolling-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/strings-test.ts b/server/sonar-ui-common/helpers/__tests__/strings-test.ts index 2a9da6cd6a9..00711cf1c1d 100644 --- a/server/sonar-ui-common/helpers/__tests__/strings-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/strings-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or diff --git a/server/sonar-ui-common/helpers/__tests__/urls-test.ts b/server/sonar-ui-common/helpers/__tests__/urls-test.ts index 171b066ae66..1eb75452a8d 100644 --- a/server/sonar-ui-common/helpers/__tests__/urls-test.ts +++ b/server/sonar-ui-common/helpers/__tests__/urls-test.ts @@ -1,6 +1,6 @@ /* - * Sonar UI Common - * Copyright (C) 2019-2020 SonarSource SA + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA * mailto:info AT sonarsource DOT com * * This program is free software; you can redistribute it and/or |