-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from vtex-apps/patch/add-tests
Patch/add tests
- Loading branch information
Showing
20 changed files
with
218 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export function findCSSHandles( | ||
container: HTMLElement, | ||
handles: readonly string[] | ||
) { | ||
const foundNodes = handles | ||
.map(handle => { | ||
const foundNodesInner = container.getElementsByClassName(handle) | ||
return foundNodesInner.length > 0 ? handle : '' | ||
}) | ||
.filter(result => result !== '') | ||
|
||
return foundNodes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function useCssHandles(input: string[]) { | ||
const acc: Record<string, string> = {} | ||
input.forEach(value => { | ||
acc[value] = value | ||
}) | ||
|
||
return acc | ||
} | ||
|
||
export const applyModifiers = (input: string, modifier: string) => { | ||
return modifier ? `${input}--${modifier}` : input | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const canUseDOM = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react' | ||
import { render, fireEvent } from '@vtex/test-tools/react' | ||
|
||
// eslint-disable-next-line jest/no-mocks-import | ||
import { findCSSHandles } from '../__mocks__/testUtils' | ||
import Backdrop, { CSS_HANDLES } from '../components/Backdrop' | ||
|
||
describe('<Backdrop />', () => { | ||
it('should render', () => { | ||
const { baseElement } = render(<Backdrop open />) | ||
|
||
expect(baseElement).toBeTruthy() | ||
}) | ||
|
||
it('should find all declared handles', () => { | ||
const { container } = render(<Backdrop open />) | ||
|
||
const foundHandles = findCSSHandles(container, CSS_HANDLES) | ||
expect(foundHandles).toEqual(CSS_HANDLES) | ||
}) | ||
|
||
it('should have visibility hidden and opacity 0 if open === false', () => { | ||
const { getByTestId } = render(<Backdrop open={false} />) | ||
|
||
const container = getByTestId('modal-backdrop-container') | ||
|
||
expect(container.style.getPropertyValue('opacity')).toBe('0') | ||
expect(container.style.getPropertyValue('visibility')).toBe('hidden') | ||
}) | ||
|
||
it('should call onClick when backdrop is clicked', () => { | ||
const spy = jest.fn() | ||
const { getByRole } = render(<Backdrop open onClick={spy} />) | ||
|
||
const backdropElement = getByRole('presentation') | ||
fireEvent.click(backdropElement, { bubbles: true, cancelable: true }) | ||
|
||
expect(spy).toBeCalledTimes(1) | ||
}) | ||
|
||
it('should have a role="presentation"', () => { | ||
const spy = jest.fn() | ||
const { queryByRole } = render(<Backdrop open onClick={spy} />) | ||
|
||
const container = queryByRole('presentation') | ||
expect(container).toBeTruthy() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import React from 'react' | ||
import { render, fireEvent } from '@vtex/test-tools/react' | ||
|
||
import BaseModal from '../BaseModal' | ||
import Fade from '../components/Animations/Fade' | ||
import { BackdropMode } from '../components/Backdrop' | ||
|
||
describe('<BaseModal />', () => { | ||
it('should render the modal with the children', () => { | ||
const onClose = jest.fn() | ||
const { queryByText, queryByRole } = render( | ||
<BaseModal open onClose={onClose} backdrop={BackdropMode.none}> | ||
<Fade in> | ||
<div>Hello VTEX</div> | ||
</Fade> | ||
</BaseModal> | ||
) | ||
|
||
expect(queryByText('Hello VTEX')).toBeTruthy() | ||
expect(queryByRole('presentation')).toBeTruthy() | ||
}) | ||
|
||
it("shouldn't render anything if open is false", () => { | ||
const { queryByText, queryByRole } = render( | ||
<BaseModal open={false} onClose={() => {}}> | ||
<Fade in> | ||
<div>Hello VTEX</div> | ||
</Fade> | ||
</BaseModal> | ||
) | ||
|
||
expect(queryByText('Hello VTEX')).toBeNull() | ||
expect(queryByRole('presentation')).toBeNull() | ||
}) | ||
|
||
it('should call onClose if Esc is pressed', () => { | ||
const onClose = jest.fn() | ||
const { getByRole } = render( | ||
<BaseModal open onClose={onClose} backdrop={BackdropMode.none}> | ||
<Fade in> | ||
<div>Hello VTEX</div> | ||
</Fade> | ||
</BaseModal> | ||
) | ||
|
||
fireEvent.keyDown(getByRole('presentation'), { | ||
key: 'Escape', | ||
code: 27, | ||
}) | ||
|
||
expect(onClose).toBeCalledTimes(1) | ||
}) | ||
|
||
it("shouldn't call onClose if disableEscapeKeyDown", () => { | ||
const onClose = jest.fn() | ||
const { getByTestId } = render( | ||
<BaseModal open onClose={onClose} disableEscapeKeyDown> | ||
<Fade in> | ||
<div data-testid="base-modal-child">Hello VTEX</div> | ||
</Fade> | ||
</BaseModal> | ||
) | ||
|
||
fireEvent.keyDown(getByTestId('base-modal-child'), { | ||
key: 'Escape', | ||
code: 27, | ||
}) | ||
|
||
expect(onClose).not.toBeCalled() | ||
}) | ||
|
||
it('should have role="presentation"', () => { | ||
const onClose = jest.fn() | ||
const { queryByRole } = render( | ||
<BaseModal open onClose={onClose} backdrop={BackdropMode.none}> | ||
<Fade in> | ||
<div>Hello VTEX</div> | ||
</Fade> | ||
</BaseModal> | ||
) | ||
|
||
expect(queryByRole('presentation')).toBeTruthy() | ||
}) | ||
|
||
it('should not propagate click events to outside of the modal', () => { | ||
const onClose = jest.fn() | ||
const outsideClick = jest.fn() | ||
const onClick = jest.fn() | ||
|
||
const { getByTestId } = render( | ||
// eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events | ||
<div onClick={outsideClick}> | ||
<BaseModal open onClick={onClick} onClose={onClose}> | ||
<Fade in> | ||
<div>Hello VTEX</div> | ||
</Fade> | ||
</BaseModal> | ||
</div> | ||
) | ||
|
||
const baseModal = getByTestId('base-modal') | ||
|
||
fireEvent.click(baseModal, { | ||
bubbles: true, | ||
cancelable: true, | ||
}) | ||
|
||
expect(onClick).toBeCalledTimes(1) | ||
expect(outsideClick).not.toBeCalled() | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,10 @@ | |
"@types/react": "^16.9.2", | ||
"@types/react-transition-group": "^4.2.3", | ||
"@vtex/test-tools": "^1.1.0", | ||
"typescript": "3.8.3" | ||
"typescript": "3.8.3", | ||
"vtex.css-handles": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles", | ||
"vtex.render-runtime": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.render-runtime", | ||
"vtex.store-icons": "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-icons" | ||
}, | ||
"version": "0.4.1" | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,5 @@ | ||
/* Typings for `render-runtime` */ | ||
declare module 'vtex.render-runtime' { | ||
import { Component, ComponentType, ReactElement, ReactType } from 'react' | ||
|
||
export interface NavigationOptions { | ||
page: string | ||
params?: any | ||
} | ||
|
||
export interface RenderContextProps { | ||
runtime: { | ||
navigate: (options: NavigationOptions) => void | ||
} | ||
} | ||
|
||
interface ExtensionPointProps { | ||
id: string | ||
[key: string]: any | ||
} | ||
import 'vtex.render-runtime' | ||
|
||
export const ExtensionPoint: ComponentType<ExtensionPointProps> | ||
|
||
interface ChildBlockProps { | ||
id: string | ||
} | ||
|
||
export const ChildBlock: ComponentType<ChildBlockProps> | ||
export const useChildBlock = function({ id: string }): object {} | ||
|
||
export const Helmet: ReactElement | ||
export const Link: ReactType | ||
export const NoSSR: ReactElement | ||
export const RenderContextConsumer: ReactElement | ||
export const canUseDOM: boolean | ||
export const withRuntimeContext: <TOriginalProps extends {}>( | ||
Component: ComponentType<TOriginalProps & RenderContextProps> | ||
) => ComponentType<TOriginalProps> | ||
declare module 'vtex.render-runtime' { | ||
const canUseDOM: boolean | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4678,6 +4678,18 @@ [email protected]: | |
core-util-is "1.0.2" | ||
extsprintf "^1.2.0" | ||
|
||
"vtex.css-handles@http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles": | ||
version "0.4.2" | ||
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.css-handles#62ee61d1bb9efdc919e5cf4bb44fabcf9050d255" | ||
|
||
"vtex.render-runtime@http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.render-runtime": | ||
version "8.100.4" | ||
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.render-runtime#b94c966be2d1c3d8bc9f1caf2dd9f462d88e98d2" | ||
|
||
"vtex.store-icons@http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-icons": | ||
version "0.17.0" | ||
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/[email protected]/public/@types/vtex.store-icons#1da7ec2f89522d8c5ed3418fec8fbc82817499db" | ||
|
||
w3c-hr-time@^1.0.1: | ||
version "1.0.2" | ||
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.2.tgz#0a89cdf5cc15822df9c360543676963e0cc308cd" | ||
|