diff options
Diffstat (limited to 'spec/frontend/lib/utils')
-rw-r--r-- | spec/frontend/lib/utils/color_utils_spec.js | 18 | ||||
-rw-r--r-- | spec/frontend/lib/utils/datetime/date_format_utility_spec.js | 15 | ||||
-rw-r--r-- | spec/frontend/lib/utils/is_navigating_away_spec.js | 23 | ||||
-rw-r--r-- | spec/frontend/lib/utils/text_utility_spec.js | 21 | ||||
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 35 |
5 files changed, 112 insertions, 0 deletions
diff --git a/spec/frontend/lib/utils/color_utils_spec.js b/spec/frontend/lib/utils/color_utils_spec.js index c6b88b2957c..87966cf9fba 100644 --- a/spec/frontend/lib/utils/color_utils_spec.js +++ b/spec/frontend/lib/utils/color_utils_spec.js @@ -1,4 +1,5 @@ import { + isValidColorExpression, textColorForBackground, hexToRgb, validateHexColor, @@ -72,4 +73,21 @@ describe('Color utils', () => { }, ); }); + + describe('isValidColorExpression', () => { + it.each` + colorExpression | valid | desc + ${'#F00'} | ${true} | ${'valid'} + ${'rgba(0,0,0,0)'} | ${true} | ${'valid'} + ${'hsl(540,70%,50%)'} | ${true} | ${'valid'} + ${'red'} | ${true} | ${'valid'} + ${'F00'} | ${false} | ${'invalid'} + ${'F00'} | ${false} | ${'invalid'} + ${'gba(0,0,0,0)'} | ${false} | ${'invalid'} + ${'hls(540,70%,50%)'} | ${false} | ${'invalid'} + ${'hello'} | ${false} | ${'invalid'} + `('color expression $colorExpression is $desc', ({ colorExpression, valid }) => { + expect(isValidColorExpression(colorExpression)).toBe(valid); + }); + }); }); diff --git a/spec/frontend/lib/utils/datetime/date_format_utility_spec.js b/spec/frontend/lib/utils/datetime/date_format_utility_spec.js index 942ba56196e..1adc70450e8 100644 --- a/spec/frontend/lib/utils/datetime/date_format_utility_spec.js +++ b/spec/frontend/lib/utils/datetime/date_format_utility_spec.js @@ -118,3 +118,18 @@ describe('date_format_utility.js', () => { }); }); }); + +describe('formatTimeAsSummary', () => { + it.each` + unit | value | result + ${'months'} | ${1.5} | ${'1.5M'} + ${'weeks'} | ${1.25} | ${'1.5w'} + ${'days'} | ${2} | ${'2d'} + ${'hours'} | ${10} | ${'10h'} + ${'minutes'} | ${20} | ${'20m'} + ${'seconds'} | ${10} | ${'<1m'} + ${'seconds'} | ${0} | ${'-'} + `('will format $value $unit to $result', ({ unit, value, result }) => { + expect(utils.formatTimeAsSummary({ [unit]: value })).toBe(result); + }); +}); diff --git a/spec/frontend/lib/utils/is_navigating_away_spec.js b/spec/frontend/lib/utils/is_navigating_away_spec.js new file mode 100644 index 00000000000..e1230fe96bf --- /dev/null +++ b/spec/frontend/lib/utils/is_navigating_away_spec.js @@ -0,0 +1,23 @@ +import { isNavigatingAway, setNavigatingForTestsOnly } from '~/lib/utils/is_navigating_away'; + +describe('isNavigatingAway', () => { + beforeEach(() => { + // Make sure each test starts with the same state + setNavigatingForTestsOnly(false); + }); + + it.each([false, true])('it returns the navigation flag with value %s', (flag) => { + setNavigatingForTestsOnly(flag); + expect(isNavigatingAway()).toEqual(flag); + }); + + describe('when the browser starts navigating away', () => { + it('returns true', () => { + expect(isNavigatingAway()).toEqual(false); + + window.dispatchEvent(new Event('beforeunload')); + + expect(isNavigatingAway()).toEqual(true); + }); + }); +}); diff --git a/spec/frontend/lib/utils/text_utility_spec.js b/spec/frontend/lib/utils/text_utility_spec.js index 1f3659b5c76..9570d2a831c 100644 --- a/spec/frontend/lib/utils/text_utility_spec.js +++ b/spec/frontend/lib/utils/text_utility_spec.js @@ -363,4 +363,25 @@ describe('text_utility', () => { expect(textUtils.insertFinalNewline(input, '\r\n')).toBe(output); }); }); + + describe('escapeShellString', () => { + it.each` + character | input | output + ${'"'} | ${'";echo "you_shouldnt_run_this'} | ${'\'";echo "you_shouldnt_run_this\''} + ${'$'} | ${'$IFS'} | ${"'$IFS'"} + ${'\\'} | ${'evil-branch-name\\'} | ${"'evil-branch-name\\'"} + ${'!'} | ${'!event'} | ${"'!event'"} + `( + 'should not escape the $character character but wrap in single-quotes', + ({ input, output }) => { + expect(textUtils.escapeShellString(input)).toBe(output); + }, + ); + + it("should escape the ' character and wrap in single-quotes", () => { + expect(textUtils.escapeShellString("fix-'bug-behavior'")).toBe( + "'fix-'\\''bug-behavior'\\'''", + ); + }); + }); }); diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index 6f186ba3227..18b68d91e01 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -1004,4 +1004,39 @@ describe('URL utility', () => { expect(urlUtils.isSameOriginUrl(url)).toBe(expected); }); }); + + describe('constructWebIDEPath', () => { + let originalGl; + const projectIDEPath = '/foo/bar'; + const sourceProj = 'my_-fancy-proj/boo'; + const targetProj = 'boo/another-fancy-proj'; + const mrIid = '7'; + + beforeEach(() => { + originalGl = window.gl; + window.gl = { webIDEPath: projectIDEPath }; + }); + + afterEach(() => { + window.gl = originalGl; + }); + + it.each` + sourceProjectFullPath | targetProjectFullPath | iid | expectedPath + ${undefined} | ${undefined} | ${undefined} | ${projectIDEPath} + ${undefined} | ${undefined} | ${mrIid} | ${projectIDEPath} + ${undefined} | ${targetProj} | ${undefined} | ${projectIDEPath} + ${undefined} | ${targetProj} | ${mrIid} | ${projectIDEPath} + ${sourceProj} | ${undefined} | ${undefined} | ${projectIDEPath} + ${sourceProj} | ${targetProj} | ${undefined} | ${projectIDEPath} + ${sourceProj} | ${undefined} | ${mrIid} | ${`/-/ide/project/${sourceProj}/merge_requests/${mrIid}?target_project=`} + ${sourceProj} | ${sourceProj} | ${mrIid} | ${`/-/ide/project/${sourceProj}/merge_requests/${mrIid}?target_project=`} + ${sourceProj} | ${targetProj} | ${mrIid} | ${`/-/ide/project/${sourceProj}/merge_requests/${mrIid}?target_project=${encodeURIComponent(targetProj)}`} + `( + 'returns $expectedPath for "$sourceProjectFullPath + $targetProjectFullPath + $iid"', + ({ expectedPath, ...args } = {}) => { + expect(urlUtils.constructWebIDEPath(args)).toBe(expectedPath); + }, + ); + }); }); |