I’m just getting started with testing and I’m learning. I wrote a small component that truncates and expands text and wrote some tests for it. I’d appreciate any feedback to help me improve. The only change I was thinking of making was using Fakerjs to generate the lorem ipsum on the fly instead of hard coding it. Thanks!
// app/components/util/truncate/component.js
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class TruncateComponent extends Component {
@tracked readMore = false;
get trimmedContent() {
return this.args.content.substring(0, this.length);
}
/**
* Checks if content needs to be trimmed
* @return {boolean} trimmed
*/
get contentTrimmed() {
return this.args.content.length > this.length;
}
/**
* Gets the length of the content
* @return {number} length
*/
get length() {
return this.args.length || 200;
}
get showReadMore() {
if('readMore' in this.args && !this.args.readMore) {
return false;
}
if(this.args.content.length < this.length) {
return false;
}
return true;
}
@action
toggleReadMore(evt) {
evt.preventDefault();
this.readMore = this.readMore ? false : true;
}
}
// app/components/util/truncate/template.hbs
{{#if this.readMore}}
{{@content}}
{{else}}
{{this.trimmedContent}}{{if this.contentTrimmed '…' ''}}
{{/if}}
{{#if this.showReadMore}}
<a {{on "click" (fn this.toggleReadMore)}} href="#">
{{#if this.readMore}}
Read Less
{{else}}
Read More
{{/if}}
</a>
{{/if}}
// tests/integration/component/util/truncate/component-test.js
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { click, render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
module('Integration | Component | util/truncate', function(hooks) {
setupRenderingTest(hooks);
test('it renders', async function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.set('myAction', function(val) { ... });
await render(hbs`<Util::Truncate @content="Sample text" />`);
assert.equal(this.element.textContent.trim(), 'Sample text');
});
test('it truncates long text', async function(assert) {
var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque neque metus, condimentum at ipsum a, feugiat ullamcorper enim. Nulla finibus at nulla ac faucibus. Fusce sodales id diam eget fermentum. Fusce auctor, diam maximus imperdiet interdum, risus mauris volutpat tortor, a aliquam urna velit ac nisi. Morbi eget congue justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse a rhoncus felis."
var includes = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque neque metus, condimentum at ipsum a, feugiat ullamcorper enim. Nulla finibus at nulla ac faucibus. Fusce sodales id diam eget ferm…';
this.set('lorem', lorem);
await render(hbs`<Util::Truncate @content="{{this.lorem}}" />`);
assert.equal(this.element.textContent.includes(includes), true);
});
test('it shows short text', async function(assert) {
var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque neque metus, condimentum at ipsum a, feugiat ullamcorper enim."
this.set('lorem', lorem);
await render(hbs`<Util::Truncate @content="{{this.lorem}}" />`);
assert.equal(this.element.textContent.trim(), lorem);
});
test('it toggles text', async function(assert) {
var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque neque metus, condimentum at ipsum a, feugiat ullamcorper enim. Nulla finibus at nulla ac faucibus. Fusce sodales id diam eget fermentum. Fusce auctor, diam maximus imperdiet interdum, risus mauris volutpat tortor, a aliquam urna velit ac nisi. Morbi eget congue justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse a rhoncus felis."
var includes = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque neque metus, condimentum at ipsum a, feugiat ullamcorper enim. Nulla finibus at nulla ac faucibus. Fusce sodales id diam eget ferm…';
this.set('lorem', lorem);
await render(hbs`<Util::Truncate @content="{{this.lorem}}" />`);
assert.equal(this.element.textContent.includes(includes), true);
await click('a');
assert.equal(this.element.textContent.includes(lorem), true);
await click('a');
assert.equal(this.element.textContent.includes(includes), true);
});
test('it allows custom length', async function(assert) {
var lorem = "Lorem ipsum dolor sit amet."
var includes = 'Lorem…';
this.set('lorem', lorem);
await render(hbs`<Util::Truncate @content="{{this.lorem}}" @length={{5}} />`);
assert.equal(this.element.textContent.includes(includes), true);
});
test('it hides read more', async function(assert) {
var lorem = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque neque metus, condimentum at ipsum a, feugiat ullamcorper enim. Nulla finibus at nulla ac faucibus. Fusce sodales id diam eget fermentum. Fusce auctor, diam maximus imperdiet interdum, risus mauris volutpat tortor, a aliquam urna velit ac nisi. Morbi eget congue justo. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse a rhoncus felis."
this.set('lorem', lorem);
await render(hbs`<Util::Truncate @content="{{this.lorem}}" @readMore={{false}} />`);
assert.equal(this.element.textContent.includes('Read More'), false);
});
});