Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add truncate test #343

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}