diff options
author | Winnie Hellmann <winnie@gitlab.com> | 2019-03-23 17:52:35 +0100 |
---|---|---|
committer | Winnie Hellmann <winnie@gitlab.com> | 2019-03-23 17:53:46 +0100 |
commit | 514ee63826e47229bfd03bdbb740f2dd1eae1d03 (patch) | |
tree | 3f0d96a4402e8aa54c375084cc4c5e6cf546824b /spec/frontend/locale/sprintf_spec.js | |
parent | 6d330015dfdb1979a0773c87c53b84cc86b28a6d (diff) | |
download | gitlab-ce-514ee63826e47229bfd03bdbb740f2dd1eae1d03.tar.gz |
Move some tests from Karma to Jest
Diffstat (limited to 'spec/frontend/locale/sprintf_spec.js')
-rw-r--r-- | spec/frontend/locale/sprintf_spec.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/spec/frontend/locale/sprintf_spec.js b/spec/frontend/locale/sprintf_spec.js new file mode 100644 index 00000000000..52e903b819f --- /dev/null +++ b/spec/frontend/locale/sprintf_spec.js @@ -0,0 +1,74 @@ +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 <script>alert("malicious!")</script>'); + }); + + it('does not escape parameters for escapeParameters = false', () => { + const input = 'contains %{safeContent}'; + const parameters = { + safeContent: '<strong>bold attempt</strong>', + }; + + const output = sprintf(input, parameters, false); + + expect(output).toBe('contains <strong>bold attempt</strong>'); + }); + }); +}); |