Skip to content

Commit

Permalink
add DatasetAlerts unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Oct 9, 2023
1 parent 117a745 commit 548119f
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { DatasetAlerts } from '../../../../../src/sections/dataset/dataset-alerts/DatasetAlerts'
import { faker } from '@faker-js/faker'

import {
DatasetAlert,
DatasetAlertMessageKey
} from '../../../../../src/dataset/domain/models/Dataset'

describe('DatasetAlerts', () => {
function removeMarkup(htmlString: string): string {
// Use a regular expression to match HTML tags and replace them with an empty string
return htmlString.replace(/<\/?[^>]+(>|$)/g, '')
}

const alerts = [
new DatasetAlert('warning', DatasetAlertMessageKey.DRAFT_VERSION),
new DatasetAlert('warning', DatasetAlertMessageKey.REQUESTED_VERSION_NOT_FOUND, {
requestedVersion: 4.0,
returnedVersion: 2.0
}),
new DatasetAlert('info', DatasetAlertMessageKey.UNPUBLISHED_DATASET, {
privateUrl: faker.internet.url()
})
]

it('renders the correct number of alerts', () => {
cy.mount(<DatasetAlerts alerts={alerts} />)
cy.findByText('Unpublished Dataset Private URL').should('exist')
const alertElements = cy.findAllByRole('alert').should('have.length', alerts.length)
})

it('renders alerts with correct content', () => {
cy.fixture('../../../public/locales/en/dataset.json').then((dataset) => {
cy.mount(<DatasetAlerts alerts={alerts} />)

cy.findAllByRole('alert').should('exist')
cy.findAllByRole('alert').should(
'contain.text',
removeMarkup(dataset.alerts.draftVersion.alertText)
)
cy.findByText('Information').should('exist')
})
})

it('renders alerts with correct headings', () => {
cy.fixture('../../../public/locales/en/dataset.json').then((dataset) => {
cy.mount(<DatasetAlerts alerts={alerts} />)

alerts.forEach((alert) => {
const alertHeading = removeMarkup(dataset.alerts[alert.message].heading)
console.log(JSON.stringify(alertHeading))
cy.findAllByRole('alert').should('contain.text', alertHeading)
})
})
})
it('renders dynamic text', () => {
cy.fixture('../../../public/locales/en/dataset.json').then((dataset) => {
const dynamicFields = {
requestedVersion: 4.0,
returnedVersion: 2.0
}
const notFoundAlert = new DatasetAlert(
'warning',
DatasetAlertMessageKey.REQUESTED_VERSION_NOT_FOUND,
dynamicFields
)
cy.mount(<DatasetAlerts alerts={[notFoundAlert]} />)

alerts.forEach((alert) => {
cy.findAllByRole('alert').should('contain.text', dynamicFields.requestedVersion)
cy.findAllByRole('alert').should('contain.text', dynamicFields.returnedVersion)
})
})
})
})

0 comments on commit 548119f

Please sign in to comment.