Skip to content

Commit

Permalink
test: add tests for cases with __DEV__ variable
Browse files Browse the repository at this point in the history
  • Loading branch information
natterstefan committed Nov 19, 2019
1 parent 28ff9fe commit 1cc4d37
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 38 deletions.
12 changes: 6 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
collectCoverageFrom: ["src/**/*.ts"],
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testPathIgnorePatterns: ["<rootDir>/(dist|es|esm|lib|node_modules)/"],
collectCoverageFrom: ['src/**/*.ts'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
testPathIgnorePatterns: ['<rootDir>/(dist|es|esm|lib|node_modules)/'],
transform: {
"^.+\\.(t|j)sx?$": "ts-jest"
}
};
'^.+\\.(t|j)sx?$': 'ts-jest',
},
}
1 change: 1 addition & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import Adapter from 'enzyme-adapter-react-16'

configure({ adapter: new Adapter() })

// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
global.__DEV__ = true
92 changes: 61 additions & 31 deletions src/components/__tests__/catalog-component.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,49 +194,79 @@ describe('CatalogComponent', () => {
expect(wrapper.find(CatalogComponent).html()).toBeNull()
})

it('tells the developer, when the requested component does not exist and no fallbackComponent was provided', () => {
mount(
<CatalogProvider catalog={testCatalog}>
<CatalogComponent component="NotAvailableComponent" />
</CatalogProvider>,
it('renders null, when the catalog context does not exist', () => {
const wrapper = mount(
<CatalogComponent component="NotAvailableComponent" />,
)

expect(wrapper.find(CatalogComponent).html()).toBeNull()

// test if the developer was notified
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenLastCalledWith(
'CatalogComponent: "NotAvailableComponent" not found in component catalog.',
'The catalog contains only:',
components,
'catalog is not defined. Please, use <CatalogComponent /> in the context of a <CatalogProvider /> with an existing catalog.',
)
})

it('tells the developer, when the requested component does not exist and no fallbackComponent was provided even when catalog is empty', () => {
mount(
<CatalogProvider catalog={emptyTestCatalog}>
<CatalogComponent component="NotAvailableComponent" />
</CatalogProvider>,
)
describe('debugging on DEV', () => {
it('tells the developer, when the requested component does not exist and no fallbackComponent was provided', () => {
mount(
<CatalogProvider catalog={testCatalog}>
<CatalogComponent component="NotAvailableComponent" />
</CatalogProvider>,
)

// test if the developer was notified
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenLastCalledWith(
'CatalogComponent: "NotAvailableComponent" not found in component catalog.',
'The catalog contains only:',
components,
)
})

// test if the developer was notified
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenLastCalledWith(
'CatalogComponent: "NotAvailableComponent" not found in component catalog.',
'The catalog contains only:',
{},
)
it('tells the developer, when the requested component does not exist and no fallbackComponent was provided even when catalog is empty', () => {
mount(
<CatalogProvider catalog={emptyTestCatalog}>
<CatalogComponent component="NotAvailableComponent" />
</CatalogProvider>,
)

// test if the developer was notified
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenLastCalledWith(
'CatalogComponent: "NotAvailableComponent" not found in component catalog.',
'The catalog contains only:',
{},
)
})
})

it('renders null, when the catalog context does not exist', () => {
const wrapper = mount(
<CatalogComponent component="NotAvailableComponent" />,
)
describe('debugging on PRODUCTION', () => {
beforeAll(() => {
;(global as any).__DEV__ = false
})

expect(wrapper.find(CatalogComponent).html()).toBeNull()
afterAll(() => {
;(global as any).__DEV__ = true
})

// test if the developer was notified
expect(console.error).toHaveBeenCalledTimes(1)
expect(console.error).toHaveBeenLastCalledWith(
'catalog is not defined. Please, use <CatalogComponent /> in the context of a <CatalogProvider /> with an existing catalog.',
)
it('does not tell enable debugging logs when __DEV__ is false', () => {
mount(
<CatalogProvider catalog={testCatalog}>
<CatalogComponent component="NotAvailableComponent" />
</CatalogProvider>,
)
expect(console.error).toHaveBeenCalledTimes(0)
})

it('does not tell enable debuggding logs when __DEV__ is false', () => {
mount(
<CatalogProvider catalog={null}>
<div />
</CatalogProvider>,
)
expect(console.error).toHaveBeenCalledTimes(0)
})
})
})
2 changes: 1 addition & 1 deletion src/components/catalog-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const CatalogComponent = React.forwardRef((props: IProps, ref) => {
}

if (fallbackComponent) {
let FallbackComponent = null
let FallbackComponent: IProps['fallbackComponent'] = null

if (typeof fallbackComponent === 'string') {
FallbackComponent = catalog.getComponent(fallbackComponent)
Expand Down

0 comments on commit 1cc4d37

Please sign in to comment.