diff options
Diffstat (limited to 'spec/frontend/lib')
-rw-r--r-- | spec/frontend/lib/utils/datetime_utility_spec.js | 119 | ||||
-rw-r--r-- | spec/frontend/lib/utils/experimentation_spec.js | 20 | ||||
-rw-r--r-- | spec/frontend/lib/utils/number_utility_spec.js | 22 | ||||
-rw-r--r-- | spec/frontend/lib/utils/select2_utils_spec.js | 100 | ||||
-rw-r--r-- | spec/frontend/lib/utils/text_markdown_spec.js | 53 | ||||
-rw-r--r-- | spec/frontend/lib/utils/unit_format/index_spec.js | 304 | ||||
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 8 |
7 files changed, 462 insertions, 164 deletions
diff --git a/spec/frontend/lib/utils/datetime_utility_spec.js b/spec/frontend/lib/utils/datetime_utility_spec.js index 32a24227cbd..2df0cb00f9a 100644 --- a/spec/frontend/lib/utils/datetime_utility_spec.js +++ b/spec/frontend/lib/utils/datetime_utility_spec.js @@ -764,6 +764,21 @@ describe('date addition/subtraction methods', () => { ); }); + describe('nYearsAfter', () => { + it.each` + date | numberOfYears | expected + ${'2020-07-06'} | ${1} | ${'2021-07-06'} + ${'2020-07-06'} | ${15} | ${'2035-07-06'} + `( + 'returns $expected for "$numberOfYears year(s) after $date"', + ({ date, numberOfYears, expected }) => { + expect(datetimeUtility.nYearsAfter(new Date(date), numberOfYears)).toEqual( + new Date(expected), + ); + }, + ); + }); + describe('nMonthsBefore', () => { // The previous month (February) has 28 days const march2019 = '2019-03-15T00:00:00.000Z'; @@ -1018,6 +1033,81 @@ describe('isToday', () => { }); }); +describe('isInPast', () => { + it.each` + date | expected + ${new Date('2024-12-15')} | ${false} + ${new Date('2020-07-06T00:00')} | ${false} + ${new Date('2020-07-05T23:59:59.999')} | ${true} + ${new Date('2020-07-05')} | ${true} + ${new Date('1999-03-21')} | ${true} + `('returns $expected for $date', ({ date, expected }) => { + expect(datetimeUtility.isInPast(date)).toBe(expected); + }); +}); + +describe('isInFuture', () => { + it.each` + date | expected + ${new Date('2024-12-15')} | ${true} + ${new Date('2020-07-07T00:00')} | ${true} + ${new Date('2020-07-06T23:59:59.999')} | ${false} + ${new Date('2020-07-06')} | ${false} + ${new Date('1999-03-21')} | ${false} + `('returns $expected for $date', ({ date, expected }) => { + expect(datetimeUtility.isInFuture(date)).toBe(expected); + }); +}); + +describe('fallsBefore', () => { + it.each` + dateA | dateB | expected + ${new Date('2020-07-06T23:59:59.999')} | ${new Date('2020-07-07T00:00')} | ${true} + ${new Date('2020-07-07T00:00')} | ${new Date('2020-07-06T23:59:59.999')} | ${false} + ${new Date('2020-04-04')} | ${new Date('2021-10-10')} | ${true} + ${new Date('2021-10-10')} | ${new Date('2020-04-04')} | ${false} + `('returns $expected for "$dateA falls before $dateB"', ({ dateA, dateB, expected }) => { + expect(datetimeUtility.fallsBefore(dateA, dateB)).toBe(expected); + }); +}); + +describe('removeTime', () => { + it.each` + date | expected + ${new Date('2020-07-07')} | ${new Date('2020-07-07T00:00:00.000')} + ${new Date('2020-07-07T00:00:00.001')} | ${new Date('2020-07-07T00:00:00.000')} + ${new Date('2020-07-07T23:59:59.999')} | ${new Date('2020-07-07T00:00:00.000')} + ${new Date('2020-07-07T12:34:56.789')} | ${new Date('2020-07-07T00:00:00.000')} + `('returns $expected for $date', ({ date, expected }) => { + expect(datetimeUtility.removeTime(date)).toEqual(expected); + }); +}); + +describe('getTimeRemainingInWords', () => { + it.each` + date | expected + ${new Date('2020-07-06T12:34:56.789')} | ${'0 days remaining'} + ${new Date('2020-07-07T12:34:56.789')} | ${'1 day remaining'} + ${new Date('2020-07-08T12:34:56.789')} | ${'2 days remaining'} + ${new Date('2020-07-12T12:34:56.789')} | ${'6 days remaining'} + ${new Date('2020-07-13T12:34:56.789')} | ${'1 week remaining'} + ${new Date('2020-07-19T12:34:56.789')} | ${'1 week remaining'} + ${new Date('2020-07-20T12:34:56.789')} | ${'2 weeks remaining'} + ${new Date('2020-07-27T12:34:56.789')} | ${'3 weeks remaining'} + ${new Date('2020-08-03T12:34:56.789')} | ${'4 weeks remaining'} + ${new Date('2020-08-05T12:34:56.789')} | ${'4 weeks remaining'} + ${new Date('2020-08-06T12:34:56.789')} | ${'1 month remaining'} + ${new Date('2020-09-06T12:34:56.789')} | ${'2 months remaining'} + ${new Date('2021-06-06T12:34:56.789')} | ${'11 months remaining'} + ${new Date('2021-07-06T12:34:56.789')} | ${'1 year remaining'} + ${new Date('2022-07-06T12:34:56.789')} | ${'2 years remaining'} + ${new Date('2030-07-06T12:34:56.789')} | ${'10 years remaining'} + ${new Date('2119-07-06T12:34:56.789')} | ${'99 years remaining'} + `('returns $expected for $date', ({ date, expected }) => { + expect(datetimeUtility.getTimeRemainingInWords(date)).toEqual(expected); + }); +}); + describe('getStartOfDay', () => { beforeEach(() => { timezoneMock.register('US/Eastern'); @@ -1046,3 +1136,32 @@ describe('getStartOfDay', () => { }, ); }); + +describe('getStartOfWeek', () => { + beforeEach(() => { + timezoneMock.register('US/Eastern'); + }); + + afterEach(() => { + timezoneMock.unregister(); + }); + + it.each` + inputAsString | options | expectedAsString + ${'2021-01-29T18:08:23.014Z'} | ${undefined} | ${'2021-01-25T05:00:00.000Z'} + ${'2021-01-29T13:08:23.014-05:00'} | ${undefined} | ${'2021-01-25T05:00:00.000Z'} + ${'2021-01-30T03:08:23.014+09:00'} | ${undefined} | ${'2021-01-25T05:00:00.000Z'} + ${'2021-01-28T18:08:23.014-10:00'} | ${undefined} | ${'2021-01-25T05:00:00.000Z'} + ${'2021-01-28T18:08:23.014-10:00'} | ${{}} | ${'2021-01-25T05:00:00.000Z'} + ${'2021-01-28T18:08:23.014-10:00'} | ${{ utc: false }} | ${'2021-01-25T05:00:00.000Z'} + ${'2021-01-28T18:08:23.014-10:00'} | ${{ utc: true }} | ${'2021-01-26T00:00:00.000Z'} + `( + 'when the provided date is $inputAsString and the options parameter is $options, returns $expectedAsString', + ({ inputAsString, options, expectedAsString }) => { + const inputDate = new Date(inputAsString); + const actual = datetimeUtility.getStartOfWeek(inputDate, options); + + expect(actual.toISOString()).toEqual(expectedAsString); + }, + ); +}); diff --git a/spec/frontend/lib/utils/experimentation_spec.js b/spec/frontend/lib/utils/experimentation_spec.js deleted file mode 100644 index 2c5d2f89297..00000000000 --- a/spec/frontend/lib/utils/experimentation_spec.js +++ /dev/null @@ -1,20 +0,0 @@ -import * as experimentUtils from '~/lib/utils/experimentation'; - -const TEST_KEY = 'abc'; - -describe('experiment Utilities', () => { - describe('isExperimentEnabled', () => { - it.each` - experiments | value - ${{ [TEST_KEY]: true }} | ${true} - ${{ [TEST_KEY]: false }} | ${false} - ${{ def: true }} | ${false} - ${{}} | ${false} - ${null} | ${false} - `('returns correct value of $value for experiments=$experiments', ({ experiments, value }) => { - window.gon = { experiments }; - - expect(experimentUtils.isExperimentEnabled(TEST_KEY)).toEqual(value); - }); - }); -}); diff --git a/spec/frontend/lib/utils/number_utility_spec.js b/spec/frontend/lib/utils/number_utility_spec.js index 2f8f1092612..4dcd9211697 100644 --- a/spec/frontend/lib/utils/number_utility_spec.js +++ b/spec/frontend/lib/utils/number_utility_spec.js @@ -9,6 +9,7 @@ import { median, changeInPercent, formattedChangeInPercent, + isNumeric, } from '~/lib/utils/number_utils'; describe('Number Utils', () => { @@ -162,4 +163,25 @@ describe('Number Utils', () => { expect(formattedChangeInPercent(0, 1, { nonFiniteResult: '*' })).toBe('*'); }); }); + + describe('isNumeric', () => { + it.each` + value | outcome + ${0} | ${true} + ${12345} | ${true} + ${'0'} | ${true} + ${'12345'} | ${true} + ${1.0} | ${true} + ${'1.0'} | ${true} + ${'abcd'} | ${false} + ${'abcd100'} | ${false} + ${''} | ${false} + ${false} | ${false} + ${true} | ${false} + ${undefined} | ${false} + ${null} | ${false} + `('when called with $value it returns $outcome', ({ value, outcome }) => { + expect(isNumeric(value)).toBe(outcome); + }); + }); }); diff --git a/spec/frontend/lib/utils/select2_utils_spec.js b/spec/frontend/lib/utils/select2_utils_spec.js new file mode 100644 index 00000000000..6d601dd5ad1 --- /dev/null +++ b/spec/frontend/lib/utils/select2_utils_spec.js @@ -0,0 +1,100 @@ +import MockAdapter from 'axios-mock-adapter'; +import $ from 'jquery'; +import { setHTMLFixture } from 'helpers/fixtures'; +import waitForPromises from 'helpers/wait_for_promises'; +import axios from '~/lib/utils/axios_utils'; +import { select2AxiosTransport } from '~/lib/utils/select2_utils'; + +import 'select2/select2'; + +const TEST_URL = '/test/api/url'; +const TEST_SEARCH_DATA = { extraSearch: 'test' }; +const TEST_DATA = [{ id: 1 }]; +const TEST_SEARCH = 'FOO'; + +describe('lib/utils/select2_utils', () => { + let mock; + let resultsSpy; + + beforeEach(() => { + setHTMLFixture('<div><input id="root" /></div>'); + + mock = new MockAdapter(axios); + + resultsSpy = jest.fn().mockReturnValue({ results: [] }); + }); + + afterEach(() => { + mock.restore(); + }); + + const setupSelect2 = (input) => { + input.select2({ + ajax: { + url: TEST_URL, + quietMillis: 250, + transport: select2AxiosTransport, + data(search, page) { + return { + search, + page, + ...TEST_SEARCH_DATA, + }; + }, + results: resultsSpy, + }, + }); + }; + + const setupSelect2AndSearch = async () => { + const $input = $('#root'); + + setupSelect2($input); + + $input.select2('search', TEST_SEARCH); + + jest.runOnlyPendingTimers(); + await waitForPromises(); + }; + + describe('select2AxiosTransport', () => { + it('uses axios to make request', async () => { + // setup mock response + const replySpy = jest.fn(); + mock.onGet(TEST_URL).reply((...args) => replySpy(...args)); + + await setupSelect2AndSearch(); + + expect(replySpy).toHaveBeenCalledWith( + expect.objectContaining({ + url: TEST_URL, + method: 'get', + params: { + page: 1, + search: TEST_SEARCH, + ...TEST_SEARCH_DATA, + }, + }), + ); + }); + + it.each` + headers | pagination + ${{}} | ${{ more: false }} + ${{ 'X-PAGE': '1', 'x-next-page': 2 }} | ${{ more: true }} + `( + 'passes results and pagination to results callback, with headers=$headers', + async ({ headers, pagination }) => { + mock.onGet(TEST_URL).reply(200, TEST_DATA, headers); + + await setupSelect2AndSearch(); + + expect(resultsSpy).toHaveBeenCalledWith( + { results: TEST_DATA, pagination }, + 1, + expect.anything(), + ); + }, + ); + }); +}); diff --git a/spec/frontend/lib/utils/text_markdown_spec.js b/spec/frontend/lib/utils/text_markdown_spec.js index 43de195c702..b538257fac0 100644 --- a/spec/frontend/lib/utils/text_markdown_spec.js +++ b/spec/frontend/lib/utils/text_markdown_spec.js @@ -171,27 +171,40 @@ describe('init markdown', () => { expect(textArea.value).toEqual(text.replace(selected, `[${selected}](url)`)); }); - it.each` - key | expected - ${'['} | ${`[${selected}]`} - ${'*'} | ${`**${selected}**`} - ${"'"} | ${`'${selected}'`} - ${'_'} | ${`_${selected}_`} - ${'`'} | ${`\`${selected}\``} - ${'"'} | ${`"${selected}"`} - ${'{'} | ${`{${selected}}`} - ${'('} | ${`(${selected})`} - ${'<'} | ${`<${selected}>`} - `('generates $expected when $key is pressed', ({ key, expected }) => { - const event = new KeyboardEvent('keydown', { key }); - - textArea.addEventListener('keydown', keypressNoteText); - textArea.dispatchEvent(event); - - expect(textArea.value).toEqual(text.replace(selected, expected)); + describe('surrounds selected text with matching character', () => { + it.each` + key | expected + ${'['} | ${`[${selected}]`} + ${'*'} | ${`**${selected}**`} + ${"'"} | ${`'${selected}'`} + ${'_'} | ${`_${selected}_`} + ${'`'} | ${`\`${selected}\``} + ${'"'} | ${`"${selected}"`} + ${'{'} | ${`{${selected}}`} + ${'('} | ${`(${selected})`} + ${'<'} | ${`<${selected}>`} + `('generates $expected when $key is pressed', ({ key, expected }) => { + const event = new KeyboardEvent('keydown', { key }); + gon.markdown_surround_selection = true; + + textArea.addEventListener('keydown', keypressNoteText); + textArea.dispatchEvent(event); + + expect(textArea.value).toEqual(text.replace(selected, expected)); + + // cursor placement should be after selection + 2 tag lengths + expect(textArea.selectionStart).toBe(selectedIndex + expected.length); + }); - // cursor placement should be after selection + 2 tag lengths - expect(textArea.selectionStart).toBe(selectedIndex + expected.length); + it('does nothing if user preference disabled', () => { + const event = new KeyboardEvent('keydown', { key: '[' }); + gon.markdown_surround_selection = false; + + textArea.addEventListener('keydown', keypressNoteText); + textArea.dispatchEvent(event); + + expect(textArea.value).toEqual(text); + }); }); describe('and text to be selected', () => { diff --git a/spec/frontend/lib/utils/unit_format/index_spec.js b/spec/frontend/lib/utils/unit_format/index_spec.js index 5b2fdf1f02b..7fd273f1b58 100644 --- a/spec/frontend/lib/utils/unit_format/index_spec.js +++ b/spec/frontend/lib/utils/unit_format/index_spec.js @@ -1,157 +1,213 @@ -import { getFormatter, SUPPORTED_FORMATS } from '~/lib/utils/unit_format'; +import { + number, + percent, + percentHundred, + seconds, + milliseconds, + decimalBytes, + kilobytes, + megabytes, + gigabytes, + terabytes, + petabytes, + bytes, + kibibytes, + mebibytes, + gibibytes, + tebibytes, + pebibytes, + engineering, + getFormatter, + SUPPORTED_FORMATS, +} from '~/lib/utils/unit_format'; describe('unit_format', () => { - describe('when a supported format is provided, the returned function formats', () => { - it('numbers, by default', () => { - expect(getFormatter()(1)).toBe('1'); - }); - - it('numbers', () => { - const formatNumber = getFormatter(SUPPORTED_FORMATS.number); - - expect(formatNumber(1)).toBe('1'); - expect(formatNumber(100)).toBe('100'); - expect(formatNumber(1000)).toBe('1,000'); - expect(formatNumber(10000)).toBe('10,000'); - expect(formatNumber(1000000)).toBe('1,000,000'); - }); - - it('percent', () => { - const formatPercent = getFormatter(SUPPORTED_FORMATS.percent); + it('engineering', () => { + expect(engineering(1)).toBe('1'); + expect(engineering(100)).toBe('100'); + expect(engineering(1000)).toBe('1k'); + expect(engineering(10_000)).toBe('10k'); + expect(engineering(1_000_000)).toBe('1M'); + + expect(engineering(10 ** 9)).toBe('1G'); + }); - expect(formatPercent(1)).toBe('100%'); - expect(formatPercent(1, 2)).toBe('100.00%'); + it('number', () => { + expect(number(1)).toBe('1'); + expect(number(100)).toBe('100'); + expect(number(1000)).toBe('1,000'); + expect(number(10_000)).toBe('10,000'); + expect(number(1_000_000)).toBe('1,000,000'); - expect(formatPercent(0.1)).toBe('10%'); - expect(formatPercent(0.5)).toBe('50%'); + expect(number(10 ** 9)).toBe('1,000,000,000'); + }); - expect(formatPercent(0.888888)).toBe('89%'); - expect(formatPercent(0.888888, 2)).toBe('88.89%'); - expect(formatPercent(0.888888, 5)).toBe('88.88880%'); + it('percent', () => { + expect(percent(1)).toBe('100%'); + expect(percent(1, 2)).toBe('100.00%'); - expect(formatPercent(2)).toBe('200%'); - expect(formatPercent(10)).toBe('1,000%'); - }); + expect(percent(0.1)).toBe('10%'); + expect(percent(0.5)).toBe('50%'); - it('percentunit', () => { - const formatPercentHundred = getFormatter(SUPPORTED_FORMATS.percentHundred); + expect(percent(0.888888)).toBe('89%'); + expect(percent(0.888888, 2)).toBe('88.89%'); + expect(percent(0.888888, 5)).toBe('88.88880%'); - expect(formatPercentHundred(1)).toBe('1%'); - expect(formatPercentHundred(1, 2)).toBe('1.00%'); - - expect(formatPercentHundred(88.8888)).toBe('89%'); - expect(formatPercentHundred(88.8888, 2)).toBe('88.89%'); - expect(formatPercentHundred(88.8888, 5)).toBe('88.88880%'); + expect(percent(2)).toBe('200%'); + expect(percent(10)).toBe('1,000%'); + }); - expect(formatPercentHundred(100)).toBe('100%'); - expect(formatPercentHundred(100, 2)).toBe('100.00%'); + it('percentHundred', () => { + expect(percentHundred(1)).toBe('1%'); + expect(percentHundred(1, 2)).toBe('1.00%'); - expect(formatPercentHundred(200)).toBe('200%'); - expect(formatPercentHundred(1000)).toBe('1,000%'); - }); + expect(percentHundred(88.8888)).toBe('89%'); + expect(percentHundred(88.8888, 2)).toBe('88.89%'); + expect(percentHundred(88.8888, 5)).toBe('88.88880%'); - it('seconds', () => { - expect(getFormatter(SUPPORTED_FORMATS.seconds)(1)).toBe('1s'); - }); + expect(percentHundred(100)).toBe('100%'); + expect(percentHundred(100, 2)).toBe('100.00%'); - it('milliseconds', () => { - const formatMilliseconds = getFormatter(SUPPORTED_FORMATS.milliseconds); + expect(percentHundred(200)).toBe('200%'); + expect(percentHundred(1000)).toBe('1,000%'); + }); - expect(formatMilliseconds(1)).toBe('1ms'); - expect(formatMilliseconds(100)).toBe('100ms'); - expect(formatMilliseconds(1000)).toBe('1,000ms'); - expect(formatMilliseconds(10000)).toBe('10,000ms'); - expect(formatMilliseconds(1000000)).toBe('1,000,000ms'); - }); + it('seconds', () => { + expect(seconds(1)).toBe('1s'); + }); - it('decimalBytes', () => { - const formatDecimalBytes = getFormatter(SUPPORTED_FORMATS.decimalBytes); - - expect(formatDecimalBytes(1)).toBe('1B'); - expect(formatDecimalBytes(1, 1)).toBe('1.0B'); - - expect(formatDecimalBytes(10)).toBe('10B'); - expect(formatDecimalBytes(10 ** 2)).toBe('100B'); - expect(formatDecimalBytes(10 ** 3)).toBe('1kB'); - expect(formatDecimalBytes(10 ** 4)).toBe('10kB'); - expect(formatDecimalBytes(10 ** 5)).toBe('100kB'); - expect(formatDecimalBytes(10 ** 6)).toBe('1MB'); - expect(formatDecimalBytes(10 ** 7)).toBe('10MB'); - expect(formatDecimalBytes(10 ** 8)).toBe('100MB'); - expect(formatDecimalBytes(10 ** 9)).toBe('1GB'); - expect(formatDecimalBytes(10 ** 10)).toBe('10GB'); - expect(formatDecimalBytes(10 ** 11)).toBe('100GB'); - }); + it('milliseconds', () => { + expect(milliseconds(1)).toBe('1ms'); + expect(milliseconds(100)).toBe('100ms'); + expect(milliseconds(1000)).toBe('1,000ms'); + expect(milliseconds(10_000)).toBe('10,000ms'); + expect(milliseconds(1_000_000)).toBe('1,000,000ms'); + }); - it('kilobytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.kilobytes)(1)).toBe('1kB'); - expect(getFormatter(SUPPORTED_FORMATS.kilobytes)(1, 1)).toBe('1.0kB'); - }); + it('decimalBytes', () => { + expect(decimalBytes(1)).toBe('1B'); + expect(decimalBytes(1, 1)).toBe('1.0B'); + + expect(decimalBytes(10)).toBe('10B'); + expect(decimalBytes(10 ** 2)).toBe('100B'); + expect(decimalBytes(10 ** 3)).toBe('1kB'); + expect(decimalBytes(10 ** 4)).toBe('10kB'); + expect(decimalBytes(10 ** 5)).toBe('100kB'); + expect(decimalBytes(10 ** 6)).toBe('1MB'); + expect(decimalBytes(10 ** 7)).toBe('10MB'); + expect(decimalBytes(10 ** 8)).toBe('100MB'); + expect(decimalBytes(10 ** 9)).toBe('1GB'); + expect(decimalBytes(10 ** 10)).toBe('10GB'); + expect(decimalBytes(10 ** 11)).toBe('100GB'); + }); - it('megabytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.megabytes)(1)).toBe('1MB'); - expect(getFormatter(SUPPORTED_FORMATS.megabytes)(1, 1)).toBe('1.0MB'); - }); + it('kilobytes', () => { + expect(kilobytes(1)).toBe('1kB'); + expect(kilobytes(1, 1)).toBe('1.0kB'); + }); - it('gigabytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.gigabytes)(1)).toBe('1GB'); - expect(getFormatter(SUPPORTED_FORMATS.gigabytes)(1, 1)).toBe('1.0GB'); - }); + it('megabytes', () => { + expect(megabytes(1)).toBe('1MB'); + expect(megabytes(1, 1)).toBe('1.0MB'); + }); - it('terabytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.terabytes)(1)).toBe('1TB'); - expect(getFormatter(SUPPORTED_FORMATS.terabytes)(1, 1)).toBe('1.0TB'); - }); + it('gigabytes', () => { + expect(gigabytes(1)).toBe('1GB'); + expect(gigabytes(1, 1)).toBe('1.0GB'); + }); - it('petabytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.petabytes)(1)).toBe('1PB'); - expect(getFormatter(SUPPORTED_FORMATS.petabytes)(1, 1)).toBe('1.0PB'); - }); + it('terabytes', () => { + expect(terabytes(1)).toBe('1TB'); + expect(terabytes(1, 1)).toBe('1.0TB'); + }); - it('bytes', () => { - const formatBytes = getFormatter(SUPPORTED_FORMATS.bytes); + it('petabytes', () => { + expect(petabytes(1)).toBe('1PB'); + expect(petabytes(1, 1)).toBe('1.0PB'); + }); - expect(formatBytes(1)).toBe('1B'); - expect(formatBytes(1, 1)).toBe('1.0B'); + it('bytes', () => { + expect(bytes(1)).toBe('1B'); + expect(bytes(1, 1)).toBe('1.0B'); - expect(formatBytes(10)).toBe('10B'); - expect(formatBytes(100)).toBe('100B'); - expect(formatBytes(1000)).toBe('1,000B'); + expect(bytes(10)).toBe('10B'); + expect(bytes(100)).toBe('100B'); + expect(bytes(1000)).toBe('1,000B'); - expect(formatBytes(1 * 1024)).toBe('1KiB'); - expect(formatBytes(1 * 1024 ** 2)).toBe('1MiB'); - expect(formatBytes(1 * 1024 ** 3)).toBe('1GiB'); - }); + expect(bytes(1 * 1024)).toBe('1KiB'); + expect(bytes(1 * 1024 ** 2)).toBe('1MiB'); + expect(bytes(1 * 1024 ** 3)).toBe('1GiB'); + }); - it('kibibytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.kibibytes)(1)).toBe('1KiB'); - expect(getFormatter(SUPPORTED_FORMATS.kibibytes)(1, 1)).toBe('1.0KiB'); - }); + it('kibibytes', () => { + expect(kibibytes(1)).toBe('1KiB'); + expect(kibibytes(1, 1)).toBe('1.0KiB'); + }); - it('mebibytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.mebibytes)(1)).toBe('1MiB'); - expect(getFormatter(SUPPORTED_FORMATS.mebibytes)(1, 1)).toBe('1.0MiB'); - }); + it('mebibytes', () => { + expect(mebibytes(1)).toBe('1MiB'); + expect(mebibytes(1, 1)).toBe('1.0MiB'); + }); - it('gibibytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.gibibytes)(1)).toBe('1GiB'); - expect(getFormatter(SUPPORTED_FORMATS.gibibytes)(1, 1)).toBe('1.0GiB'); - }); + it('gibibytes', () => { + expect(gibibytes(1)).toBe('1GiB'); + expect(gibibytes(1, 1)).toBe('1.0GiB'); + }); - it('tebibytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.tebibytes)(1)).toBe('1TiB'); - expect(getFormatter(SUPPORTED_FORMATS.tebibytes)(1, 1)).toBe('1.0TiB'); - }); + it('tebibytes', () => { + expect(tebibytes(1)).toBe('1TiB'); + expect(tebibytes(1, 1)).toBe('1.0TiB'); + }); - it('pebibytes', () => { - expect(getFormatter(SUPPORTED_FORMATS.pebibytes)(1)).toBe('1PiB'); - expect(getFormatter(SUPPORTED_FORMATS.pebibytes)(1, 1)).toBe('1.0PiB'); - }); + it('pebibytes', () => { + expect(pebibytes(1)).toBe('1PiB'); + expect(pebibytes(1, 1)).toBe('1.0PiB'); }); - describe('when get formatter format is incorrect', () => { - it('formatter fails', () => { - expect(() => getFormatter('not-supported')(1)).toThrow(); + describe('getFormatter', () => { + it.each([ + [1], + [10], + [200], + [100], + [1000], + [10_000], + [100_000], + [1_000_000], + [10 ** 6], + [10 ** 9], + [0.1], + [0.5], + [0.888888], + ])('formatting functions yield the same result as getFormatter for %d', (value) => { + expect(number(value)).toBe(getFormatter(SUPPORTED_FORMATS.number)(value)); + expect(percent(value)).toBe(getFormatter(SUPPORTED_FORMATS.percent)(value)); + expect(percentHundred(value)).toBe(getFormatter(SUPPORTED_FORMATS.percentHundred)(value)); + + expect(seconds(value)).toBe(getFormatter(SUPPORTED_FORMATS.seconds)(value)); + expect(milliseconds(value)).toBe(getFormatter(SUPPORTED_FORMATS.milliseconds)(value)); + + expect(decimalBytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.decimalBytes)(value)); + expect(kilobytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.kilobytes)(value)); + expect(megabytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.megabytes)(value)); + expect(gigabytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.gigabytes)(value)); + expect(terabytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.terabytes)(value)); + expect(petabytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.petabytes)(value)); + + expect(bytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.bytes)(value)); + expect(kibibytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.kibibytes)(value)); + expect(mebibytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.mebibytes)(value)); + expect(gibibytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.gibibytes)(value)); + expect(tebibytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.tebibytes)(value)); + expect(pebibytes(value)).toBe(getFormatter(SUPPORTED_FORMATS.pebibytes)(value)); + + expect(engineering(value)).toBe(getFormatter(SUPPORTED_FORMATS.engineering)(value)); + }); + + describe('when get formatter format is incorrect', () => { + it('formatter fails', () => { + expect(() => getFormatter('not-supported')(1)).toThrow(); + }); }); }); }); diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index b60ddea81ee..e12cd8b0e37 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -814,6 +814,14 @@ describe('URL utility', () => { ); }); + it('decodes URI when decodeURI=true', () => { + const url = 'https://gitlab.com/test'; + + expect(urlUtils.setUrlParams({ labels: ['foo', 'bar'] }, url, false, true, true)).toEqual( + 'https://gitlab.com/test?labels[]=foo&labels[]=bar', + ); + }); + it('removes all existing URL params and sets a new param when cleanParams=true', () => { const url = 'https://gitlab.com/test?group_id=gitlab-org&project_id=my-project'; |