Вот пример для изменения attribute
из document.body
:
index.ts
:
export function render() {
if (document.body.getAttribute('data-basepath') === 'coolvalue') {
return 'My Component';
}
return null;
}
index.spec.ts
:
import { render } from './';
describe('render', () => {
test('should render my component', () => {
document.body.setAttribute('data-basepath', 'coolvalue');
const actualValue = render();
expect(actualValue).toBe('My Component');
});
it('should not render my component', () => {
document.body.setAttribute('data-basepath', '');
const actualValue = render();
expect(actualValue).toBeNull();
});
});
Результат модульного теста со 100% покрытием:
PASS src/stackoverflow/58365044/index.spec.ts (8.322s)
render
✓ should render my component (11ms)
✓ should not render my component (1ms)
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 10.205s