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

Snapshot testing #894

Draft
wants to merge 4 commits into
base: templates
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
2 changes: 2 additions & 0 deletions packages/snap-preact-components/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const rootConfig = require('../../jest.base.config.json');
module.exports = {
...rootConfig,
Expand All @@ -10,4 +11,5 @@ module.exports = {
},
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
testTimeout: 10000,
snapshotSerializers: ['@emotion/jest/serializer'],
};
1 change: 1 addition & 0 deletions packages/snap-preact-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"preact": "10.9.0"
},
"devDependencies": {
"@emotion/jest": "11.9.0",
"@searchspring/snap-client": "^0.47.0",
"@searchspring/snap-controller": "^0.47.0",
"@searchspring/snap-event-manager": "^0.47.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,50 @@ import { h } from 'preact';

import { render } from '@testing-library/preact';
import { ThemeProvider } from '../../../providers';

import themes from '../../../themes';
import { Badge } from './Badge';

const CONTENT = 'sale';
const CLASSNAME = 'sale-badge';
const CHILDREN = <div>{CONTENT}</div>;

describe('Badge Component', () => {
Object.keys(themes || {}).forEach((themeName) => {
it(`uses ${themeName} theme`, () => {
const theme = themes[themeName as keyof typeof themes];
const rendered = render(<Badge theme={theme}>{CHILDREN}</Badge>);
expect(rendered.asFragment()).toMatchSnapshot();
});
});
it('positions badge based on prop', () => {
const position = { right: 0 };
const rendered = render(<Badge position={position}>{CHILDREN}</Badge>);
const badge = rendered.container.querySelector('.ss__badge')!;
let styles = getComputedStyle(badge)!;

expect(styles.right).toEqual('0px');
expect(rendered.asFragment()).toMatchSnapshot();

const newPosition = { left: 0 };
rendered.rerender(<Badge position={newPosition}>{CHILDREN}</Badge>);
styles = getComputedStyle(badge);

expect(styles.left).toEqual('0px');
expect(rendered.asFragment()).toMatchSnapshot();

const bottomPosition = { bottom: 0 };
rendered.rerender(<Badge position={bottomPosition}>{CHILDREN}</Badge>);
styles = getComputedStyle(badge);

expect(styles.bottom).toEqual('0px');
expect(rendered.asFragment()).toMatchSnapshot();

const topPosition = { top: 0 };
rendered.rerender(<Badge position={topPosition}>{CHILDREN}</Badge>);
styles = getComputedStyle(badge);

expect(styles.top).toEqual('0px');
expect(rendered.asFragment()).toMatchSnapshot();
});

describe('Badge with content', () => {
Expand All @@ -47,6 +58,7 @@ describe('Badge Component', () => {
const BadgeElement = rendered.getByText(CONTENT);

expect(BadgeElement).toBeInTheDocument();
expect(rendered.asFragment()).toMatchSnapshot();
});

it('Badge has correct classes', () => {
Expand All @@ -55,6 +67,7 @@ describe('Badge Component', () => {
expect(badge.classList.length).toBe(3);
expect(badge.classList[0]).toMatch(/^ss__badge/);
expect(badge).toHaveClass(CLASSNAME);
expect(rendered.asFragment()).toMatchSnapshot();
});
});

Expand All @@ -69,13 +82,15 @@ describe('Badge Component', () => {
const BadgeElement = badge.getByText(CONTENT);

expect(BadgeElement).toBeInTheDocument();
expect(badge.asFragment()).toMatchSnapshot();
});

it('Badge has correct number of classes', () => {
const BadgeElement = badge.getByText(CONTENT).parentElement;

expect(BadgeElement.classList.length).toBe(2);
expect(BadgeElement.classList[0]).toMatch(/^ss__badge/);
expect(badge.asFragment()).toMatchSnapshot();
});
});

Expand All @@ -89,12 +104,14 @@ describe('Badge Component', () => {
const BadgeElement = badge.getByText(CONTENT);

expect(BadgeElement).toBeInTheDocument();
expect(badge.asFragment()).toMatchSnapshot();
});

it('Badge has correct number of classes', () => {
const BadgeElement = badge.getByText(CONTENT);

expect(BadgeElement?.classList.length).toBe(1);
expect(badge.asFragment()).toMatchSnapshot();
});
});

Expand All @@ -116,6 +133,7 @@ describe('Badge Component', () => {
const element = rendered.container.querySelector('.ss__badge');
expect(element).toBeInTheDocument();
expect(element).toHaveClass(globalTheme.components.badge.className);
expect(rendered.asFragment()).toMatchSnapshot();
});

it('is themeable with theme prop', () => {
Expand All @@ -135,6 +153,7 @@ describe('Badge Component', () => {
const element = rendered.container.querySelector('.ss__badge');
expect(element).toBeInTheDocument();
expect(element).toHaveClass(propTheme.components.badge.className);
expect(rendered.asFragment()).toMatchSnapshot();
});

it('is theme prop overrides ThemeProvider', () => {
Expand Down Expand Up @@ -169,6 +188,7 @@ describe('Badge Component', () => {
expect(element).not.toHaveClass(globalTheme.components.badge.className);
expect(element).toHaveTextContent(propTheme.components.badge.content);
expect(element).not.toHaveTextContent(globalTheme.components.badge.content);
expect(rendered.asFragment()).toMatchSnapshot();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Badge Component Badge theming works is theme prop overrides ThemeProvider 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
right: 0;
}

<div
class="ss__badge classier emotion-0"
>
ahhhh
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge theming works is themeable with ThemeProvider 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
right: 0;
}

<div
class="ss__badge classy emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge theming works is themeable with theme prop 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
right: 0;
}

<div
class="ss__badge classy emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge with children Badge has correct number of classes 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
right: 0;
}

<div
class="ss__badge emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge with children renders Badge 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
right: 0;
}

<div
class="ss__badge emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge with content Badge has correct classes 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
top: 0;
left: 0;
}

<div
class="ss__badge sale-badge emotion-0"
>
sale
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge with content renders Badge 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
top: 0;
left: 0;
}

<div
class="ss__badge sale-badge emotion-0"
>
sale
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge with disableStyles Badge has correct number of classes 1`] = `
<DocumentFragment>
<div
class="ss__badge"
>
sale
</div>
</DocumentFragment>
`;

exports[`Badge Component Badge with disableStyles renders Badge 1`] = `
<DocumentFragment>
<div
class="ss__badge"
>
sale
</div>
</DocumentFragment>
`;

exports[`Badge Component positions badge based on prop 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
right: 0;
}

<div
class="ss__badge emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component positions badge based on prop 2`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
left: 0;
}

<div
class="ss__badge emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component positions badge based on prop 3`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
bottom: 0;
}

<div
class="ss__badge emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component positions badge based on prop 4`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
top: 0;
}

<div
class="ss__badge emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;

exports[`Badge Component uses bocachica theme 1`] = `
<DocumentFragment>
.emotion-0 {
display: inline-block;
position: absolute;
top: 0;
left: 0;
}

<div
class="ss__badge emotion-0"
>
<div>
sale
</div>
</div>
</DocumentFragment>
`;
Loading