debian-mirror-gitlab/spec/frontend/locale/sprintf_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import sprintf from '~/locale/sprintf';
describe('locale', () => {
describe('sprintf', () => {
it('does not modify string without parameters', () => {
const input = 'No parameters';
const output = sprintf(input);
expect(output).toBe(input);
});
it('ignores extraneous parameters', () => {
const input = 'No parameters';
const output = sprintf(input, { ignore: 'this' });
expect(output).toBe(input);
});
it('ignores extraneous placeholders', () => {
const input = 'No %{parameters}';
const output = sprintf(input);
expect(output).toBe(input);
});
it('replaces parameters', () => {
const input = '%{name} has %{count} parameters';
const parameters = {
name: 'this',
count: 2,
};
const output = sprintf(input, parameters);
expect(output).toBe('this has 2 parameters');
});
it('replaces multiple occurrences', () => {
const input = 'to %{verb} or not to %{verb}';
const parameters = {
verb: 'be',
};
const output = sprintf(input, parameters);
expect(output).toBe('to be or not to be');
});
it('escapes parameters', () => {
const input = 'contains %{userContent}';
const parameters = {
userContent: '<script>alert("malicious!")</script>',
};
const output = sprintf(input, parameters);
expect(output).toBe('contains &lt;script&gt;alert(&quot;malicious!&quot;)&lt;/script&gt;');
});
it('does not escape parameters for escapeParameters = false', () => {
const input = 'contains %{safeContent}';
const parameters = {
2022-10-11 01:57:18 +05:30
safeContent: '15',
2018-03-17 18:26:18 +05:30
};
const output = sprintf(input, parameters, false);
2022-10-11 01:57:18 +05:30
expect(output).toBe('contains 15');
});
describe('replaces duplicated % in input', () => {
it('removes duplicated percentage signs', () => {
const input = 'contains duplicated %{safeContent}%%';
const parameters = {
safeContent: '15',
};
const output = sprintf(input, parameters, false);
expect(output).toBe('contains duplicated 15%');
});
2018-03-17 18:26:18 +05:30
});
});
});