Skip to content

Instantly share code, notes, and snippets.

@mauteri
Created July 16, 2021 21:54
Show Gist options
  • Save mauteri/55f87b8ae004b6abb77822b37d4fd625 to your computer and use it in GitHub Desktop.
Save mauteri/55f87b8ae004b6abb77822b37d4fd625 to your computer and use it in GitHub Desktop.
addListItem DOM Manipulation Jest Test
import {addListItem} from '../index';
describe('addListItem', () => {
beforeAll(() => {
document.body.innerHTML = '<ul id="unittest"></ul>';
const $el = document.getElementById('unittest')
addListItem($el, 'First');
addListItem($el, 'Second');
addListItem($el, 'Third');
});
afterAll(() => {
document.body.innerHTML = '';
});
test('Number of items is 3', () => {
const count = document.getElementById('unittest').getElementsByTagName('li').length;
expect(count).toBe(3);
});
test('First element has text First', () => {
const text = document.getElementById('unittest').getElementsByTagName('li')[0].textContent;
expect(text).toBe('First');
});
test('Second element has text Second', () => {
const text = document.getElementById('unittest').getElementsByTagName('li')[1].textContent;
expect(text).toBe('Second');
});
test('Third element has text Third', () => {
const text = document.getElementById('unittest').getElementsByTagName('li')[2].textContent;
expect(text).toBe('Third');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment