diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 12:26:25 +0000 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 12:26:25 +0000 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /spec/frontend/lib/utils/text_utility_spec.js | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) | |
download | gitlab-ce-a09983ae35713f5a2bbb100981116d31ce99826e.tar.gz |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'spec/frontend/lib/utils/text_utility_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/text_utility_spec.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/spec/frontend/lib/utils/text_utility_spec.js b/spec/frontend/lib/utils/text_utility_spec.js index 76e0e435860..285f7d04c3b 100644 --- a/spec/frontend/lib/utils/text_utility_spec.js +++ b/spec/frontend/lib/utils/text_utility_spec.js @@ -145,6 +145,56 @@ describe('text_utility', () => { }); }); + describe('truncate', () => { + it('returns the original string when str length is less than maxLength', () => { + const str = 'less than 20 chars'; + expect(textUtils.truncate(str, 20)).toEqual(str); + }); + + it('returns truncated string when str length is more than maxLength', () => { + const str = 'more than 10 chars'; + expect(textUtils.truncate(str, 10)).toEqual(`${str.substring(0, 10 - 1)}…`); + }); + + it('returns the original string when rendered width is exactly equal to maxWidth', () => { + const str = 'Exactly 16 chars'; + expect(textUtils.truncate(str, 16)).toEqual(str); + }); + }); + + describe('truncateWidth', () => { + const clientWidthDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'clientWidth'); + + beforeAll(() => { + // Mock measured width of ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' + Object.defineProperty(Element.prototype, 'clientWidth', { + value: 431, + writable: false, + }); + }); + + afterAll(() => { + Object.defineProperty(Element.prototype, 'clientWidth', clientWidthDescriptor); + }); + + it('returns the original string when rendered width is less than maxWidth', () => { + const str = '< 80px'; + expect(textUtils.truncateWidth(str)).toEqual(str); + }); + + it('returns truncated string when rendered width is more than maxWidth', () => { + const str = 'This is wider than 80px'; + expect(textUtils.truncateWidth(str)).toEqual(`${str.substring(0, 10)}…`); + }); + + it('returns the original string when rendered width is exactly equal to maxWidth', () => { + const str = 'Exactly 159.62962962962965px'; + expect(textUtils.truncateWidth(str, { maxWidth: 159.62962962962965, fontSize: 10 })).toEqual( + str, + ); + }); + }); + describe('truncateSha', () => { it('shortens SHAs to 8 characters', () => { expect(textUtils.truncateSha('verylongsha')).toBe('verylong'); |