Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl Andin committed Apr 12, 2024
1 parent fc958e6 commit e90bd0f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 23 deletions.
4 changes: 2 additions & 2 deletions public/js/app/components/InfoModal.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react/no-danger */
import React, { useState } from 'react'
import { Modal, ModalBody, ModalHeader, ModalFooter, Button } from 'reactstrap'
import { Modal, ModalBody, ModalHeader, ModalFooter } from 'reactstrap'

function InfoModal(props) {
const [modal, setModal] = useState(false)
Expand Down Expand Up @@ -52,7 +52,7 @@ function InfoModal(props) {
<ModalHeader
className="h-4"
toggle={toggle}
close={<button className="kth-icon-button close" onClick={toggle} />}
close={<button className="kth-icon-button close" aria-label={closeLabel || 'Close'} onClick={toggle} />}
>
{title || ''}
</ModalHeader>
Expand Down
17 changes: 9 additions & 8 deletions public/js/app/components/__tests__/InfoModal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,8 @@ describe('Component <InfoModal> with html text 2I', () => {

// Open modal
fireEvent.click(modalBtn)
await waitFor(() => {
const allBtns = screen.getAllByRole('button')
expect(allBtns[1]).toHaveTextContent('×')
})
const allBtns = screen.getAllByRole('button')
expect(allBtns[1]).toHaveAccessibleName('Close')
expect(allBtns[2]).toHaveTextContent('Close')
})

Expand All @@ -65,7 +62,7 @@ describe('Component <InfoModal> with html text 2I', () => {
})

describe('Component <InfoModal> and its functionality 3I', () => {
test('open modal and close by clicking ×', async () => {
test('open modal and close by clicking icon button', async () => {
renderInfoModal()
const modalBtn = screen.getByRole('button')

Expand All @@ -75,8 +72,10 @@ describe('Component <InfoModal> and its functionality 3I', () => {
expect(screen.getByText('Valid for')).toBeInTheDocument()
})

// Close modal using ×
const modalCrossBtn = screen.getByRole('button', { name: '×' })
// Close modal using icon button
const modalButtons = screen.getAllByRole('button', { name: 'Close' })
const modalCrossBtn = modalButtons.find(x => x.className.includes('kth-icon-button'))
expect(modalCrossBtn).toBeDefined()
fireEvent.click(modalCrossBtn)
setTimeout(async () => waitForElementToBeRemoved(() => screen.queryByText('Valid for')), 1000)
})
Expand All @@ -92,7 +91,9 @@ describe('Component <InfoModal> and its functionality 3I', () => {
})

// Close modal using "Close"
const modalCloseBtn = screen.getByRole('button', { name: /close/i })
const modalButtons = screen.getAllByRole('button', { name: 'Close' })
const modalCloseBtn = modalButtons.find(x => !x.className.includes('kth-icon-button'))
expect(modalCloseBtn).toBeDefined()
fireEvent.click(modalCloseBtn)

setTimeout(async () => waitForElementToBeRemoved(() => screen.queryByText('Valid for')), 1000)
Expand Down
31 changes: 19 additions & 12 deletions public/js/app/components/__tests__/SideMenu.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ describe('Component <SideMenu>', () => {
expect(links.length).toBe(5)

expect(links[0].href).toBe('http://localhost/student/kurser/kurser-inom-program?l=en')
expect(links[0].title).toBe('Course and programme directory')
expect(links[0]).toHaveTextContent('Course and programme directory')

expect(links[1].href).toBe('http://localhost/student/kurser/kurs/KIP1111?l=en')
expect(links[1].title).toBe('Before course selection')
expect(links[1]).toHaveTextContent('Before course selection')

expect(links[2].href).toBe('http://localhost/kurs-pm/KIP1111?l=en')
expect(links[2].title).toBe('Prepare and take course')
expect(links[2]).toHaveTextContent('Prepare and take course')

expect(links[3].href).toBe('http://localhost/kursutveckling/KIP1111?l=en')
expect(links[3].title).toBe('Course development')
expect(links[3]).toHaveTextContent('Course development')

expect(links[4].href).toBe('http://localhost/kursutveckling/KIP1111/arkiv?l=en')
expect(links[4].title).toBe('Archive')
expect(links[4]).toHaveTextContent('Archive')

expect(screen.getByText('About course KIP1111')).toBeInTheDocument()
// const a screen.getAllByText('About course KIP1111')
const navs = screen.getAllByRole('navigation')
expect(navs.length).toBe(2)
expect(navs[0]).toHaveAccessibleName('About course KIP1111')
expect(navs[1]).toHaveAccessibleName('About course KIP1111')
})

test('renders a side menu for a course and with labels. Swedish', () => {
Expand All @@ -49,20 +53,23 @@ describe('Component <SideMenu>', () => {
expect(links.length).toBe(5)

expect(links[0].href).toBe('http://localhost/student/kurser/kurser-inom-program')
expect(links[0].title).toBe('Kurs- och programkatalogen')
expect(links[0]).toHaveTextContent('Kurs- och programkatalogen')

expect(links[1].href).toBe('http://localhost/student/kurser/kurs/KIP1111')
expect(links[1].title).toBe('Inför kursval')
expect(links[1]).toHaveTextContent('Inför kursval')

expect(links[2].href).toBe('http://localhost/kurs-pm/KIP1111')
expect(links[2].title).toBe('Förbereda och gå kurs')
expect(links[2]).toHaveTextContent('Förbereda och gå kurs')

expect(links[3].href).toBe('http://localhost/kursutveckling/KIP1111?l=sv')
expect(links[3].title).toBe('Kursens utveckling')
expect(links[3]).toHaveTextContent('Kursens utveckling')

expect(links[4].href).toBe('http://localhost/kursutveckling/KIP1111/arkiv?l=sv')
expect(links[4].title).toBe('Arkiv')
expect(links[4]).toHaveTextContent('Arkiv')

expect(screen.getByText('Om kursen KIP1111')).toBeInTheDocument()
const navs = screen.getAllByRole('navigation')
expect(navs.length).toBe(2)
expect(navs[0]).toHaveAccessibleName('Om kursen KIP1111')
expect(navs[1]).toHaveAccessibleName('Om kursen KIP1111')
})
})
3 changes: 2 additions & 1 deletion public/js/app/pages/__tests__/CoursePage.test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions server/controllers/__tests__/courseCtrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jest.mock('../../configuration', () => ({
proxyPrefixPath: { uri: '/student/kurser/kurs' },
session: { sessionOptions: { secret: '' } },
sessionSecret: 'xxx',
toolbar: { url: 'toolbarUrl' },
},
browser: { session: {}, sessionSecret: 'xxx' },
}))
Expand Down

0 comments on commit e90bd0f

Please sign in to comment.