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

test: improve alert #375

Merged
merged 4 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 13 additions & 2 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
module.exports = {
presets: ['@babel/preset-env', '@babel/preset-typescript']
const testingConfig = require('./babel.test.config')

module.exports = api => {
const isTest = api.env('test');

if(isTest) return testingConfig;

return {
presets: [
'@babel/preset-env',
'@babel/preset-typescript'
]
};
};
bestguy marked this conversation as resolved.
Show resolved Hide resolved
13 changes: 13 additions & 0 deletions babel.test.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
"targets": {
"node": "current"
}
}
],
'@babel/preset-typescript'
]
}
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ module.exports = {
transformIgnorePatterns: ['<rootDir>/node_modules/(?!(@popperjs)/)'],
moduleFileExtensions: ['js', 'svelte'],
bail: false,
verbose: false
verbose: false,
setupFilesAfterEnv: ["@testing-library/jest-dom/extend-expect"]
};
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 96 additions & 8 deletions src/__test__/Alert.spec.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,119 @@
import Alert from '../Alert.svelte';
import { render, cleanup } from '@testing-library/svelte';
import { render, cleanup, fireEvent, waitForElementToBeRemoved } from '@testing-library/svelte';

beforeEach(cleanup);

describe('Alert', () => {
test('should render text and default color', () => {
const { container } = render(Alert, {
test('should render with default color and text', () => {
const { queryByRole } = render(Alert, {
props: { children: 'Hello world!' }
});
const alert = container.querySelector('.alert');

const alert = queryByRole('alert');
expect(alert.innerHTML.trim()).toBe('Hello world!');
expect(alert.className).toBe('alert alert-success');
});

test('should render specified color', () => {
const { container } = render(Alert, {
const { queryByRole } = render(Alert, {
props: { color: 'primary', children: 'Hello world!' }
});
const alert = container.querySelector('.alert');
const alert = queryByRole('alert');
expect(alert.className).toBe('alert alert-primary');
});

test('should render custom class', () => {
const { container } = render(Alert, {
const { queryByRole } = render(Alert, {
props: { color: 'danger', children: 'Hello world!', class: 'boogie' }
});
const alert = container.querySelector('.alert');
const alert = queryByRole('alert');
expect(alert.className).toBe('boogie alert alert-danger');
});

test('should render dismissible alert', async () => {
const { queryByRole, queryByLabelText } = render(Alert, {
props: { color: "info", children: 'I can be dismissed!', dismissible: true }
});

const alert = queryByRole('alert');
const closeBtn = queryByLabelText("Close");

expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();

fireEvent.click(closeBtn);

await waitForElementToBeRemoved(alert);

expect(alert).not.toBeInTheDocument();
expect(closeBtn).not.toBeInTheDocument();
});

test('should render dismissible alert', async () => {
const { queryByRole, queryByLabelText } = render(Alert, {
props: { color: "info", children: 'I can be dismissed!', dismissible: true }
});

const alert = queryByRole('alert');
const closeBtn = queryByLabelText("Close");

expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();

await fireEvent.click(closeBtn);

await waitForElementToBeRemoved(alert, { timeout: 500 });

expect(alert).not.toBeInTheDocument();
expect(closeBtn).not.toBeInTheDocument();
});

test('should render a controlled alert', async () => {
let isOpen = true;
const toggle = jest.fn(() => {
isOpen = false
});

const { rerender, queryByRole, queryByLabelText } = render(Alert, {
props: { color: "info", children: 'I can be dismissed!', isOpen, toggle }
});

const alert = queryByRole('alert');
const closeBtn = queryByLabelText("Close");

expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();

await fireEvent.click(closeBtn);

expect(isOpen).toBe(false);
expect(toggle).toHaveBeenCalledTimes(1);

await rerender({ isOpen, toggle });

expect(alert).not.toBeInTheDocument();
});

test('should render alert without fade', async () => {
const { queryByRole, queryByLabelText } = render(Alert, {
props: { color: "info", children: 'I can be dismissed!', dismissible: true, fade: false }
});

const alert = queryByRole('alert');
const closeBtn = queryByLabelText("Close");

expect(alert).toBeInTheDocument();
expect(alert.className).toBe('alert alert-info alert-dismissible');
expect(closeBtn).toBeInTheDocument();

await fireEvent.click(closeBtn);

await waitForElementToBeRemoved(alert, { timeout: 100 });

expect(alert).not.toBeInTheDocument();
expect(closeBtn).not.toBeInTheDocument();
});
});