Skip to content

Commit

Permalink
feat: tests for WithAuthority
Browse files Browse the repository at this point in the history
  • Loading branch information
awgaan authored and varl committed Mar 23, 2020
1 parent a9d33d3 commit e82fb0f
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/components/WithAuthority/WithAuthority.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react'
import { render } from 'test-utils'
import '@testing-library/jest-dom/extend-expect'

import { UserContext } from '../../contexts/'
import { WithAuthority } from '.'

const rejectAllPredicate = () => false
const testPredicate = authorities => authorities.includes('TEST')

const renderContext = (contextValue, props) =>
render(
<UserContext.Provider value={contextValue}>
<WithAuthority {...props} />
</UserContext.Provider>
)

const contextValue = { name: 'Ole', authorities: ['ABC', 'DEF', 'TEST'] }

test("doesn't render children when user has insufficient authority", () => {
const { queryByDataTest } = renderContext(contextValue, {
pred: rejectAllPredicate,
children: <div data-test="child-div"></div>,
})
const childDiv = queryByDataTest('child-div')
expect(childDiv).toEqual(null)
})

test('renders children when user has sufficient authority', () => {
const { queryByDataTest } = renderContext(contextValue, {
pred: testPredicate,
children: <div data-test="child-div"></div>,
})
const childDiv = queryByDataTest('child-div')
expect(childDiv).not.toEqual(null)
expect(childDiv).toBeInTheDocument()
expect(childDiv).toBeVisible()
})

0 comments on commit e82fb0f

Please sign in to comment.