Skip to content

Commit

Permalink
feat: add support for not and fix a couple of bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldelcore committed Apr 16, 2020
1 parent a3f0add commit caeab5e
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 11 deletions.
57 changes: 57 additions & 0 deletions packages/jest/src/__tests__/matchers.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { render } from '@testing-library/react';
import React from 'react';
import '@compiled/css-in-js';

it('should detect styles', () => {
const { getByText } = render(
<div
css={{
fontSize: '12px',
}}>
hello world
</div>
);

expect(getByText('hello world')).toHaveCompiledCss('font-size', '12px');
});

it('should detect missing styles', () => {
const { getByText } = render(<div css={{ fontSize: '12px' }}>hello world</div>);

expect(getByText('hello world')).not.toHaveCompiledCss('color', 'blue');
});

it('should detect multiple styles', () => {
const { getByText } = render(<div css={{ fontSize: '12px', color: 'blue' }}>hello world</div>);

expect(getByText('hello world')).toHaveCompiledCss({
fontSize: '12px',
color: 'blue',
});
});

it('should detect single missing styles', () => {
const { getByText } = render(<div css={{ fontSize: '12px', color: 'blue' }}>hello world</div>);

expect(getByText('hello world')).not.toHaveCompiledCss({
zindex: '9999',
});
});

it('should detect multiple missing styles', () => {
const { getByText } = render(<div css={{ fontSize: '12px', color: 'blue' }}>hello world</div>);

expect(getByText('hello world')).not.toHaveCompiledCss({
backgroundColor: 'yellow',
zindex: '9999',
});
});

it('should detect evaluated rule from array styles', () => {
const base = { fontSize: 12 };
const next = ` font-size: 15px; `;

const { getByText } = render(<div css={[base, next]}>hello world</div>);
expect(getByText('hello world')).toHaveCompiledCss('font-size', '15px');
expect(getByText('hello world')).toHaveCompiledCss('font-size', '12px');
});
41 changes: 30 additions & 11 deletions packages/jest/src/matchers.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
export const toHaveCompiledCss: jest.CustomMatcher = (
const kebabCase = (str: string) =>
str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/\s+/g, '-')
.toLowerCase();

export function toHaveCompiledCss(
this: jest.MatcherUtils,
element: HTMLElement,
...args: [{ [key: string]: string } | string, string]
) => {
): jest.CustomMatcherResult {
const [property, value] = args;
const properties = typeof property === 'string' ? { [property]: value } : property;
let styleElement = element.parentElement && element.parentElement.querySelector('style');
Expand All @@ -17,10 +24,6 @@ export const toHaveCompiledCss: jest.CustomMatcher = (
}
}

const stylesToFind = Object.keys(properties).map(
property => `${property}:${properties[property]}`
);

if (!styleElement) {
return {
pass: false,
Expand All @@ -36,18 +39,34 @@ export const toHaveCompiledCss: jest.CustomMatcher = (
let css = styleElement.textContent || '';

if (styles && Object.keys(styles).length > 0) {
Object.entries(styles).forEach(([key, value]: any) => {
Object.entries(styles).forEach(([key, value]: [string, any]) => {
// Replace all instances of var with the value.
// We split and join to replace all instances without needing to jump into a dynamic regex.
css = css.split(`var(${key})`).join(value);
});
}

const stylesToFind = Object.keys(properties).map(
property => `${kebabCase(property)}:${properties[property]}`
);
const foundStyles = stylesToFind.filter(styleToFind => css.includes(styleToFind));
const notFoundStyles = stylesToFind.filter(styleToFind => !css.includes(styleToFind));
const includedSelector = css.includes(`.${element.className}`);

if (css.includes(`.${element.className}`) && notFoundStyles.length === 0) {
if (includedSelector && foundStyles.length > 0 && notFoundStyles.length === 0) {
return {
pass: true,
message: () => '',
message: !this.isNot
? () => ''
: () => `Found "${foundStyles.join(', ')}" on <${element.nodeName.toLowerCase()} ${styles &&
`style={${JSON.stringify(styles)}}`}> element.
Reconciled css (css variables replaced with actual values):
${css}
Original css:
${(styleElement && styleElement.textContent) || ''}
`,
};
}

Expand All @@ -58,11 +77,11 @@ export const toHaveCompiledCss: jest.CustomMatcher = (
)}" on <${element.nodeName.toLowerCase()} ${styles &&
`style={${JSON.stringify(styles)}}`}> element.
Reconciled css (css variables were replaced with actual values):
Reconciled css (css variables replaced with actual values):
${css}
Original css:
${(styleElement && styleElement.textContent) || ''}
`,
};
};
}

0 comments on commit caeab5e

Please sign in to comment.