Skip to content

Commit

Permalink
Update GroupActivities test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksellen committed Aug 29, 2022
1 parent 6931d0f commit 041bf14
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
48 changes: 31 additions & 17 deletions src/activities/pages/GroupActivities.spec.js
Expand Up @@ -12,7 +12,7 @@ import {
createGroup,
loginAs,
setPageSize,
createPlace, createActivity, createActivityType,
createPlace, createActivity, createActivityType, db,
} from '>/mockBackend'
import { translatableActivityTypeNames } from '>/mockBackend/activityTypes'
import { addUserToGroup } from '>/mockBackend/groups'
Expand All @@ -22,9 +22,7 @@ import { sample } from '>/mockBackend/offers'
import GroupActivities from './GroupActivities'

describe('GroupActivities', () => {
let places
let activities
let activityTypes
let user, group, places, activities, activityTypes
useMockBackend()

beforeEach(() => {
Expand All @@ -33,24 +31,24 @@ describe('GroupActivities', () => {
})

beforeEach(() => {
const user = createUser()
const group = createGroup()
user = createUser()
group = createGroup()
addUserToGroup(user, group)
user.currentGroup = group.id
setPageSize(3) // make it have to do pagination stuff too...
places = times(3, () => createPlace({ group: group.id }))
places = times(2, () => createPlace({ group: group.id }))
activityTypes = times(
3,
idx => createActivityType({
// make sure we get a few different types
name: translatableActivityTypeNames[idx],
group: group.id,
}),
)
activities = times(
5,
// make sure each activity type gets a few activities
activityTypes.length * 3,
idx => createActivity({
// ensure we use each place and activity type at least once
// (keep number of activities higher than either)
place: places[idx % places.length].id,
activityType: activityTypes[idx % activityTypes.length].id,
}),
Expand Down Expand Up @@ -102,11 +100,12 @@ describe('GroupActivities', () => {

it('can sign up for activity', async () => {
setPageSize(100) // don't want to deal with pagination here
const { findByText, getByRole, findByRole, findAllByTestId } = render(GroupActivities, withDefaults())
const { findByText, getByRole, findByRole, findAllByRole } = render(GroupActivities, withDefaults())

// find a slot we can sign into and click it!
const joinSlots = await findAllByTestId('join-slot')
await fireEvent.click(sample(joinSlots))
const activityType = sample(activityTypes)
const joinButtons = await findAllByRole('button', { name: `Join ${activityType.name}` })
await fireEvent.click(sample(joinButtons))

// confirm our attendance
await findByText(/Are you sure you have time/)
Expand All @@ -116,11 +115,26 @@ describe('GroupActivities', () => {
await require('@/base/queryClient').default.invalidateQueries()

// when we're signed up we get a nice Download ICS link
// TODO: is there another way to find out we signed up?? filter by slots=joined?
await findByRole('link', { name: 'Download ICS' })

// one less slot to join
const updatedJoinSlots = await findAllByTestId('join-slot')
expect(updatedJoinSlots.length).toEqual(joinSlots.length - 1)
// peek at the db to see if we really joined it...
const joinedActivities = db.activities.filter(activity => activity.participants.some(participant => participant.user === user.id))
expect(joinedActivities).toHaveLength(1)

// one less activity to join
const updatedJoinButtons = await findAllByRole('button', { name: `Join ${activityType.name}` })
expect(updatedJoinButtons.length).toEqual(joinButtons.length - 1)

// let's go look at our joined activities
await fireEvent.click(await findByText('All')) // the slots filter
await fireEvent.click(await findByText('Joined'))

await flushPromises() // needs a bit of thinking time to update...

// We should only have one entry now...
const chatButtons = await findAllByRole('button', { name: 'Open Chat' })
expect(chatButtons).toHaveLength(1)
})

// TODO: add a test to try joining as different participant types
})
25 changes: 20 additions & 5 deletions test/mockBackend/activities.js
Expand Up @@ -5,6 +5,7 @@ import { ctx, db } from '>/mockBackend/index'
import { cursorPaginated, post } from '>/mockBackend/mockAxios'

let nextId = 1
let nextParticipantTypeId = 1
export function generateActivity (params = {}) {
if (!params.place) throw new Error('must provide place')
if (!params.activityType) {
Expand All @@ -24,7 +25,7 @@ export function generateActivity (params = {}) {
description: faker.lorem.paragraphs(2),
series: null,
place: null,
maxParticipants: 2,
participantTypes: [{ id: nextParticipantTypeId++, role: 'member', maxParticipants: 2 }],
participants: [],
feedbackDue: addDays(endDate, 30), // TODO: is this about right?
feedbackGivenBy: [],
Expand All @@ -51,20 +52,34 @@ export function createMockActivitiesBackend () {
if (params.series && activity.series !== params.series) return false
if (params.dateMin && activity.date[0] < params.dateMin) return false
if (params.activityType && activity.activityType !== params.activityType) return false
// TODO: implement feedbackPossible and slots
if (params.slots) {
if (params.slots === 'joined') {
if (!activity.participants.some(participant => participant.user === ctx.authUser.id)) return false
}
else {
throw new Error(`have not implemented slots=${params.slots} filter`)
}
}
if (params.feedbackPossible) throw new Error('have not implemented feedbackPossible filter')
return true
}).map(toResponse),
)

post('/api/activities/:id/add/', ({ pathParams }) => {
post('/api/activities/:id/add/', ({ pathParams, data }) => {
const activity = db.orm.activities.get({ id: parseInt(pathParams.id) })
if (activity.participants.includes(ctx.authUser.id)) {
if (activity.participants.map(participant => participant.user).includes(ctx.authUser.id)) {
return [403, {
detail: 'You have already joined this activity.',
error_code: 'permission_denied',
}]
}
activity.participants.push(ctx.authUser.id)
// TODO: this is probably a required param?
const { participantType } = data
activity.participants.push({
user: ctx.authUser.id,
participantType,
createdAt: new Date(),
})
return [200, {}]
})
}
1 change: 1 addition & 0 deletions test/mockBackend/groups.js
Expand Up @@ -41,6 +41,7 @@ export function addUserToGroup (user, group, membershipParams = {}) {
createdAt: faker.date.past(),
addedBy: null,
roles: [
'member',
'editor',
],
active: true,
Expand Down

0 comments on commit 041bf14

Please sign in to comment.