Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
kanekotic committed Nov 30, 2019
1 parent 8653142 commit 05ad88b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
3 changes: 1 addition & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ const SyncController = (system: ActorSystem, config: any): Router => {

router.post(`${config.paths.push}/:id`, async (req: Request, res: Response) => {
try {
let actor: any = await system.resolveOrNew(req.params.id, config.actorTypes[req.body.type], [req.params.id])
const actor: any = await system.resolveOrNew(req.params.id, config.actorTypes[req.body.type], [req.params.id])
actor.updateFrom(req.body)
res.sendStatus(200)
} catch (error) {
res.send(500)
}

})

return router
Expand Down
32 changes: 4 additions & 28 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,23 @@ describe('index exports function that returns express router', () => {
})

describe('push', () => {
it('should update state if actor exists', async () => {
const id = faker.random.uuid(),
body = { stuff: faker.random.uuid() },
ActorSystemMock = {
actorFor: jest.fn(),
},
ActorMock = {
updateFrom: jest.fn(),
}

let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockResolvedValue(ActorMock)

const result = await supertest.post(`${config.paths.push}/${id}`).send(body)

expect(ActorSystemMock.actorFor).toHaveBeenCalledWith(id)
expect(ActorMock.updateFrom).toHaveBeenCalledWith(body)
expect(result.statusCode).toEqual(200)
})

it('should crate actor and initialize state if actor does not exists', async () => {
it('should update state of an actor', async () => {
const id = faker.random.uuid(),
body = { stuff: faker.random.uuid(), type: 'FakeActor' },
ActorSystemMock = {
actorFor: jest.fn(),
actorOf: jest.fn(),
resolveOrNew: jest.fn(),
},
ActorMock = {
updateFrom: jest.fn(),
}

let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockRejectedValue('')
ActorSystemMock.actorOf.mockResolvedValue(ActorMock)
ActorSystemMock.resolveOrNew.mockResolvedValue(ActorMock)

const result = await supertest.post(`${config.paths.push}/${id}`).send(body)

expect(ActorSystemMock.actorFor).toHaveBeenCalledWith(id)
expect(ActorSystemMock.actorOf).toHaveBeenCalledWith(FakeActor, [id])
expect(ActorSystemMock.resolveOrNew).toHaveBeenCalledWith(id, FakeActor, [id])
expect(ActorMock.updateFrom).toHaveBeenCalledWith(body)
expect(result.statusCode).toEqual(200)
})
Expand Down

0 comments on commit 05ad88b

Please sign in to comment.