Skip to content

Commit

Permalink
unit tests updated for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelDau committed May 7, 2024
1 parent 5dc37a8 commit 6ca28de
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ exports[`Notification component loads notifications from cookies on mount 1`] =
</div>
`;

exports[`Notification component renders null when document.cookie is null 1`] = `
exports[`Notification component renders nothing when document.cookie is null 1`] = `
<div
className="notification-none"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,21 @@ describe('Notification component', () => {
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
})
test('renders nothing when document.cookie is null', () => {
const onDataFromNotification = jest.fn()
const originalDocument = document.cookie
Object.defineProperty(document, 'cookie', { value: null, writable: true })

const reusableComponent = <Notification onDataFromNotification={onDataFromNotification} />
let component
act(() => {
component = create(reusableComponent)
})
const tree = component.toJSON()
expect(tree).toMatchSnapshot()

document.cookie = originalDocument
})
test('loads notifications from cookies on mount', () => {
const onDataFromNotification = jest.fn()
const notificationValue = [{ key: 1, text: 'Test Notification', title: 'Test Title' }]
Expand All @@ -25,9 +39,7 @@ describe('Notification component', () => {
const tree = component.toJSON()

expect(tree).toMatchSnapshot()
expect(document.cookie).toBe(
`notifications=${encodeURIComponent(JSON.stringify(notificationValue))};`
)
expect(document.cookie).toBe(`notifications=${JSON.stringify(notificationValue)}`)
})

test('handles click on exit button and updates state and cookie', () => {
Expand Down Expand Up @@ -90,51 +102,29 @@ describe('Notification component', () => {
tree = component.toJSON()
expect(tree).toMatchSnapshot()

if (elementToExit) {
expect(elementToExit.style.display).toBe('undefined')
}
expect(elementToExit).toBe(undefined)
})
test('renders null when there are no notifications but document.cookie is not null', () => {
const onDataFromNotification = jest.fn()
const reusableComponent = <Notification onDataFromNotification={onDataFromNotification} />
const originalDocument = document.cookie
let component
const cookieValue = 'otherrandomdata=otherrandomdata'
document.cookie = cookieValue
act(() => {
component = create(reusableComponent)
})
const tree = component.toJSON()
expect(tree).toMatchSnapshot()

if (document && document.cookie) {
const cookiePropsRaw = decodeURIComponent(document.cookie).split(';')
expect(document).not.toBeNull()
expect(document.cookie).not.toBeNull()

cookiePropsRaw.forEach(c => {
const parts = c.trim().split('=')

if (parts[0] === 'notifications') {
//don't get here
} else {
expect(parts[0]).not.toBe('notifications')
}
})
} else {
expect(document.cookie).toBe(undefined)
}
})
test('renders null when document.cookie is null', () => {
const onDataFromNotification = jest.fn()
const originalDocument = document.cookie
document.cookie = null

const reusableComponent = <Notification onDataFromNotification={onDataFromNotification} />
let component
act(() => {
component = create(reusableComponent)
})
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
const cookiePropsRaw = decodeURIComponent(document.cookie).split(';')

//expect(document.cookie).toBe(null)
const parts = cookiePropsRaw[0].trim().split('=')

expect(parts[1]).toBe('undefined')
document.cookie = originalDocument
})
test('does not update cookie when there are no notifications', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,13 @@ describe('RepositoryNav', () => {
})

test('loads notifications from cookies on mount', () => {
document.cookie =
'notifications=' +
JSON.stringify([{ key: 1, text: 'Test Notification', title: 'Test Title' }])

const notificationValue = { key: 1, text: 'TestNotification', title: 'TestTitle' }
document.cookie = `notifications=${JSON.stringify(notificationValue)}`
const component = create(<RepositoryNav {...navProps} />)
const tree = component.toJSON()

expect(tree).toMatchSnapshot()
expect(document.cookie).toBe(
'notifications=[{"key":1,"text":"Test Notification","title":"Test Title"}]'
)
expect(document.cookie).toBe(`notifications=${JSON.stringify(notificationValue)}`)
})

test('renders null when document.cookie is null', () => {
Expand Down Expand Up @@ -304,8 +300,8 @@ describe('RepositoryNav', () => {
const reusableComponent = <RepositoryNav {...navProps} />
let component
let parsedValue
const notificationValue = 'otherrandomdata=otherrandomdata'
document.cookie = notificationValue
const cookieValue = 'otherrandomdata=otherrandomdata'
document.cookie = cookieValue
act(() => {
component = create(reusableComponent)
})
Expand All @@ -323,7 +319,7 @@ describe('RepositoryNav', () => {
expect(parts[0]).not.toBe('notifications')
expect(parts[0] === 'notifications').toBe(false)
})
expect(document.cookie).toBe(notificationValue)
expect(document.cookie).toBe(cookieValue)
expect(parsedValue).toBe(undefined)
})
})

0 comments on commit 6ca28de

Please sign in to comment.