Skip to content

Commit

Permalink
add truncate test
Browse files Browse the repository at this point in the history
  • Loading branch information
mazmassa committed Oct 9, 2023
1 parent ed2b603 commit 4ae8035
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
30 changes: 30 additions & 0 deletions cypress/component/unit/truncate.cy.tsx
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) + '...',
);
});
});
});
4 changes: 2 additions & 2 deletions src/util/truncate.ts
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;
}

0 comments on commit 4ae8035

Please sign in to comment.