-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/// <reference types="cypress" /> | ||
|
||
import { truncate } from '../../../src/util/truncate'; | ||
|
||
describe('Component | Unit | Util | Truncate', () => { | ||
before(() => { | ||
// check if the import worked correctly | ||
expect(truncate, 'truncate').to.be.a('function'); | ||
}); | ||
|
||
context('truncate', function () { | ||
it('should truncate content and add "..." after "n" limit of chars', () => { | ||
expect(truncate('abcdefghij', 3)).to.be.eq('abc...'); | ||
}); | ||
|
||
it('should display raw content when content is smaller than limit', () => { | ||
expect(truncate('abc', 3)).to.be.eq('abc'); | ||
expect(truncate('abc', 4)).to.be.eq('abc'); | ||
expect(truncate('abc', 2)).to.not.be.eq('abc'); | ||
}); | ||
|
||
it('should truncate as 80 chars as limit by default', () => { | ||
const longSentence = 'A'.repeat(81); | ||
|
||
expect(truncate(longSentence)).to.be.eq( | ||
longSentence.slice(0, -1) + '...', | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// 80 is the max str length | ||
export function truncate(content: string) { | ||
return content.length > 80 ? `${content.slice(0, 80)}...` : content; | ||
export function truncate(content: string, limit = 80) { | ||
return content.length > limit ? `${content.slice(0, limit)}...` : content; | ||
} |