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

Tests: Improve the Code Coverage initiative using Jest #480

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 16.x
- run: npm ci
- run: npm test
86 changes: 86 additions & 0 deletions __tests__/modal.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const Modal = require('../src/components/modal').default;

test('Modal constructor throws an error without a target element', () => {
expect(() => new Modal()).toThrow('TargetEl is required for the use of Modal component.');
});

describe('Modal class', () => {
let modal;

beforeEach(() => {
const targetEl = document.createElement('div');
document.body.appendChild(targetEl);
modal = new Modal(targetEl, { _isHidden: false });
});

afterEach(() => {
modal._targetEl.parentNode.removeChild(modal._targetEl);
});

test('constructor initializes', () => {
expect(modal._targetEl).toBeDefined();
expect(modal._isHidden).toBe(true);
});

test('show method shows the modal', () => {
modal.show();
expect(modal._isHidden).toBe(false);
expect(modal.isVisible()).toBe(true);
});

test('hide method hides the modal', () => {
modal.hide();
expect(modal._isHidden).toBe(true);
expect(modal.isHidden()).toBe(true);
});

test('should be closed if escape key is pressed', () => {
const event = new Event('keydown', { keyCode: 27 });
document.dispatchEvent(event);
expect(modal._isHidden).toBe(true);
});

test('if closable is false, escape key should not close it', () => {
modal._options.closable = false;
modal.show();
const event = new Event('keydown', { keyCode: 27 });
document.dispatchEvent(event);
expect(modal._isHidden).toBe(false);
});

test('if backdrop is dynamic, escape key should not close it', () => {
modal._options.backdrop = 'dynamic';
modal.show();
const event = new Event('keydown', { keyCode: 27 });
document.dispatchEvent(event);
expect(modal._isHidden).toBe(false);
});

test('if modal is open, clicking outside of it should close it', () => {
const event = new Event('click');
document.dispatchEvent(event);
expect(modal._isHidden).toBe(true);
});

test('if closable is false, clicking outside of it should not close it', () => {
modal._options.closable = false;
modal.show();
const event = new Event('click');
document.dispatchEvent(event);
expect(modal._isHidden).toBe(false);
});

test('if backdrop is dynamic, clicking outside of it should not close it', () => {
modal._options.backdrop = 'dynamic';
modal.show();
const event = new Event('click');
document.dispatchEvent(event);
expect(modal._isHidden).toBe(false);
});

test('should create backdrop element when shown', () => {
expect(modal._backdropEl).toBeNull();
modal.show();
expect(modal._backdropEl).not.toBeNull();
});
});
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jest-environment-jsdom',
};
Loading