event emitter test
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { WorkspaceRoles } from "@speckle/shared"
|
||||
import { WorkspaceRoles } from '@speckle/shared'
|
||||
|
||||
export type Workspace = {
|
||||
id: string
|
||||
@@ -12,4 +12,3 @@ export type Workspace = {
|
||||
}
|
||||
|
||||
export type WorkspaceAcl = { userId: string; role: WorkspaceRoles; workspaceId: string }
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import { initializeModuleEventEmitter } from "@/modules/shared/services/moduleEventEmitterSetup"
|
||||
import { WorkspaceEvents, WorkspaceEventsPayloads } from "@/modules/workspaces/domain/events"
|
||||
import { initializeModuleEventEmitter } from '@/modules/shared/services/moduleEventEmitterSetup'
|
||||
import {
|
||||
WorkspaceEvents,
|
||||
WorkspaceEventsPayloads
|
||||
} from '@/modules/workspaces/domain/events'
|
||||
|
||||
const { emit, listen } = initializeModuleEventEmitter<WorkspaceEventsPayloads>({ moduleName: 'workspaces' })
|
||||
const { emit, listen } = initializeModuleEventEmitter<WorkspaceEventsPayloads>({
|
||||
moduleName: 'workspaces'
|
||||
})
|
||||
|
||||
export const WorkspacesEmitter = { emit, listen, events: WorkspaceEvents }
|
||||
export const WorkspacesEmitter = { emit, listen, events: WorkspaceEvents }
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
import { StoreWorkspace, UpsertWorkspaceRole } from '@/modules/workspaces/domain/operations'
|
||||
import {
|
||||
StoreWorkspace,
|
||||
UpsertWorkspaceRole
|
||||
} from '@/modules/workspaces/domain/operations'
|
||||
import { Workspace, WorkspaceAcl } from '@/modules/workspaces/domain/types'
|
||||
import { Roles } from '@speckle/shared'
|
||||
import { Knex } from 'knex'
|
||||
@@ -10,19 +13,18 @@ const tables = {
|
||||
|
||||
export const storeWorkspaceFactory =
|
||||
({ db }: { db: Knex }): StoreWorkspace =>
|
||||
async ({ workspace }) => {
|
||||
await tables.workspaces(db).insert(workspace)
|
||||
}
|
||||
async ({ workspace }) => {
|
||||
await tables.workspaces(db).insert(workspace)
|
||||
}
|
||||
|
||||
// TODO: Authorise requester for given role change operation?
|
||||
export const upsertWorkspaceRole =
|
||||
({ db }: { db: Knex }): UpsertWorkspaceRole =>
|
||||
async ({ userId, workspaceId, role }) => {
|
||||
const validRoles = Object.values(Roles.Workspace)
|
||||
if (!validRoles.includes(role)) {
|
||||
throw new Error(`Unexpected workspace role provided: ${role}`)
|
||||
}
|
||||
|
||||
await tables.workspacesAcl(db).insert({ userId, workspaceId, role })
|
||||
async ({ userId, workspaceId, role }) => {
|
||||
const validRoles = Object.values(Roles.Workspace)
|
||||
if (!validRoles.includes(role)) {
|
||||
throw new Error(`Unexpected workspace role provided: ${role}`)
|
||||
}
|
||||
|
||||
await tables.workspacesAcl(db).insert({ userId, workspaceId, role })
|
||||
}
|
||||
|
||||
@@ -57,6 +57,39 @@ describe('Workspace creation', () => {
|
||||
role: Roles.Workspace.Admin
|
||||
})
|
||||
})
|
||||
it('emits a workspace created event', async () => {})
|
||||
it('emits a workspace created event', async () => {
|
||||
const eventData = {
|
||||
isCalled: false,
|
||||
event: '',
|
||||
payload: {}
|
||||
}
|
||||
const createWorkspace = createWorkspaceFactory({
|
||||
storeWorkspace: async () => {},
|
||||
upsertWorkspaceRole: async () => {},
|
||||
emitWorkspaceEvent: async ({ event, payload }) => {
|
||||
eventData.isCalled = true
|
||||
eventData.event = event
|
||||
eventData.payload = payload
|
||||
return []
|
||||
},
|
||||
storeBlob: async () => cryptoRandomString({ length: 10 })
|
||||
})
|
||||
|
||||
const workspaceInput = {
|
||||
description: 'foobar',
|
||||
logo: null,
|
||||
name: cryptoRandomString({ length: 6 })
|
||||
}
|
||||
const userId = cryptoRandomString({ length: 10 })
|
||||
|
||||
const workspace = await createWorkspace({
|
||||
userId,
|
||||
workspaceInput
|
||||
})
|
||||
|
||||
expect(eventData.isCalled).to.equal(true)
|
||||
expect(eventData.payload).to.equal('created')
|
||||
expect(eventData.payload).to.deep.equal(workspace)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user