Skip to content

Commit

Permalink
e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
Wnt committed Nov 1, 2024
1 parent 442a47d commit 8ad6717
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
1 change: 1 addition & 0 deletions frontend/src/e2e-test/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare global {
captureScreenshots: (namePrefix: string) => Promise<void>
saveTraces: (namePrefix: string) => Promise<void>
promises: Promise<void>[]
keepSessionAliveThrottleTime?: number
}
| undefined
}
Expand Down
21 changes: 19 additions & 2 deletions frontend/src/e2e-test/pages/citizen/citizen-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export default class CitizenMessagesPage {
)
.assertTextEquals('Viesti lähetetty')
}
async assertThreadMessagesEqual(messages: string[]) {
await this.#threadContent.assertTextsEqual(messages)
}

async assertThreadContent(message: {
title: string
Expand Down Expand Up @@ -148,16 +151,30 @@ export default class CitizenMessagesPage {
async assertReplyContentIsEmpty() {
return this.#messageReplyContent.assertTextEquals('')
}

async replyToFirstThread(content: string) {
await this.startReplyToFirstThread()
await this.#messageReplyContent.fill(content)
await this.sendReply()
}
async startReplyToFirstThread() {
await this.#threadListItem.click()
await this.#openReplyEditorButton.click()
await this.#messageReplyContent.fill(content)
}
getReplyContentElement() {
return this.#messageReplyContent
}
async sendReply() {
await this.#sendReplyButton.click()
// the editor is hidden after sending the reply
await this.#sendReplyButton.waitUntilHidden()
}

async getSessionExpiry() {
const cookies = await this.page.page.context().cookies()
const sessionCookie = cookies.find((c) => c.name === 'evaka.eugw.session')
return sessionCookie?.expires ?? 0
}

async deleteFirstThread() {
await this.#threadListItem.findByDataQa('delete-thread-btn').click()
}
Expand Down
43 changes: 43 additions & 0 deletions frontend/src/e2e-test/specs/7_messaging/messaging.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,49 @@ describe('Sending and receiving messages', () => {
)
})

test('Citizen session is kept alive as long as user keeps typing', async () => {
await openSupervisorPage(mockedDateAt10)
await unitSupervisorPage.goto(`${config.employeeUrl}/messages`)
const messagesPage = new MessagesPage(unitSupervisorPage)
const messageEditor = await messagesPage.openMessageEditor()
await messageEditor.sendNewMessage(defaultMessage)
await runPendingAsyncJobs(mockedDateAt10.addMinutes(1))

await openCitizen(mockedDateAt11)

await citizenPage.goto(config.enduserMessagesUrl)
await citizenPage.page.evaluate(() => {
if (window.evaka) window.evaka.keepSessionAliveThrottleTime = 300 // Set to 300ms for tests
})
const citizenMessagesPage = new CitizenMessagesPage(citizenPage)
await citizenMessagesPage.assertThreadContent(defaultMessage)

const initialExpiry = await citizenMessagesPage.getSessionExpiry()

await citizenMessagesPage.startReplyToFirstThread()

const slowTypedText =
'Olen aika hidas kirjoittamaan näitä viestejä, mutta yritän parhaani: Lapseni ovat sitä ja tätä...'

const msDelayPerCharToTypeSlowly = 1000 / slowTypedText.length
const authStatusRequests: string[] = []
citizenPage.page.on('request', (request) => {
if (request.url().includes('/api/application/auth/status')) {
authStatusRequests.push(request.url())
}
})
// typing this takes so long that session keepalive mechanism should renew the session
await citizenMessagesPage
.getReplyContentElement()
.locator.pressSequentially(slowTypedText, {
delay: msDelayPerCharToTypeSlowly
})

const finalExpiry = await citizenMessagesPage.getSessionExpiry()
expect(authStatusRequests.length).toBeGreaterThanOrEqual(3)
expect(finalExpiry).toBeGreaterThan(initialExpiry + 0.3)
})

describe('Messages can be deleted / archived', () => {
test('Unit supervisor sends message and citizen deletes the message', async () => {
await openSupervisorPage(mockedDateAt10)
Expand Down
7 changes: 4 additions & 3 deletions frontend/src/lib-components/useKeepSessionAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { useState, useEffect, useMemo } from 'react'

export function useKeepSessionAlive(sessionKeepAlive: () => Promise<boolean>) {
const [showSessionExpiredModal, setShowSessionExpiredModal] = useState(false)

const throttleTime =
window.evaka?.keepSessionAliveThrottleTime ?? 2 * 60 * 1000
const keepSessionAlive = useMemo(
() =>
throttle(
Expand All @@ -23,13 +24,13 @@ export function useKeepSessionAlive(sessionKeepAlive: () => Promise<boolean>) {
}
},
// Default to 2 minutes and allow overriding this in automated tests
window.evaka?.keepSessionAliveThrottleTime ?? 2 * 60 * 1000,
throttleTime,
{
leading: true,
trailing: true
}
),
[sessionKeepAlive]
[sessionKeepAlive, throttleTime]
)

useEffect(() => {
Expand Down

0 comments on commit 8ad6717

Please sign in to comment.