Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add tests
  • Loading branch information
sarayourfriend committed Jul 5, 2021
commit 65c9714c09857b0dd3e9cdb2a9ef6c76731ca56e
1 change: 1 addition & 0 deletions packages/components/src/utils/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export { default as useControlledState } from './use-controlled-state';
export { default as useJumpStep } from './use-jump-step';
export { default as useUpdateEffect } from './use-update-effect';
export { useControlledValue } from './use-controlled-value';
export { useCx } from './use-cx';
64 changes: 64 additions & 0 deletions packages/components/src/utils/hooks/test/use-cx.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import { cx as innerCx } from '@emotion/css';
import { insertStyles } from '@emotion/utils';
import { render } from '@testing-library/react';
import { css, CacheProvider } from '@emotion/react';
import createCache from '@emotion/cache';

/**
* Internal dependencies
*/
import { useCx } from '..';

jest.mock( '@emotion/css', () => ( {
cx: jest.fn(),
} ) );

jest.mock( '@emotion/utils', () => ( {
insertStyles: jest.fn(),
} ) );

function Example( { args } ) {
const cx = useCx();

return <div className={ cx( ...args ) } />;
}

describe( 'useCx', () => {
it( 'should call cx with the built style name and pass serialized styles to insertStyles', () => {
const serializedStyle = css`
color: red;
`;
const className = 'component-example';
const object = {
'component-example-focused': true,
};

const key = 'test-cache-key';

const container = document.createElement( 'head' );

const cache = createCache( { container, key } );

render(
<CacheProvider value={ cache }>
<Example args={ [ className, serializedStyle, object ] } />
</CacheProvider>
);

expect( innerCx ).toHaveBeenCalledWith(
className,
`${ key }-${ serializedStyle.name }`,
object
);

expect( insertStyles ).toHaveBeenCalledWith(
cache,
serializedStyle,
false
);
} );
} );