Skip to content

Commit

Permalink
fix lint issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kanekotic committed Dec 30, 2018
1 parent ba4f6ab commit 800b418
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
16 changes: 9 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
import { Router, Request, Response } from 'express'
import { Request, Response, Router } from 'express'
import { ActorSystem } from 'tarant'

export default function SyncController(system: ActorSystem, config: any) {
const SyncController = (system: ActorSystem, config: any): Router => {
const router: Router = Router()

router.get(`${config.paths.pull}/:id`, async (req: Request, res: Response) => {
try {
const actor = (await system.actorFor(req.params.id)) as any
res.json(await actor.toJson())
const actor = (await system.actorFor(req.params.id)) as any
res.json(await actor.toJson())
} catch (_) {
res.sendStatus(404)
}
})

router.post(`${config.paths.push}/:id`, async (req: Request, res: Response) => {
let actor
let actor
try {
actor = (await system.actorFor(req.params.id)) as any
actor = (await system.actorFor(req.params.id)) as any
} catch (_) {
actor = (await system.actorOf(config.ActorTypes[req.body.type], [req.params.id])) as any
actor = (await system.actorOf(config.ActorTypes[req.body.type], [req.params.id])) as any
}
actor.updateFrom(req.body)
res.sendStatus(200)
})

return router
}

export default SyncController
126 changes: 62 additions & 64 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import SyncController from '../lib/index'
import { ActorSystem } from 'tarant'
import Actor from 'tarant/dist/actor-system/actor'
import * as request from 'supertest'
import * as express from "express"
import * as express from 'express'
import * as faker from 'faker'
import { json } from "body-parser";
import { json } from 'body-parser'

class FakeActor extends Actor {}
let config: any = {
Expand All @@ -28,93 +28,91 @@ describe('index exports function that returns express router', () => {
supertest = request(app)
})

function setupController(system: ActorSystem){
function setupController(system: ActorSystem) {
const controller = SyncController(system, config)
app.use(controller)
}

describe('pull', () => {
it('should pull actor data if found', async () => {

const id = faker.random.uuid(),
expectedresult = { stuff: faker.random.uuid() },
ActorSystemMock = {
actorFor: jest.fn()
const id = faker.random.uuid(),
expectedresult = { stuff: faker.random.uuid() },
ActorSystemMock = {
actorFor: jest.fn(),
}

let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockResolvedValue({
toJson: () => Promise.resolve(expectedresult)
})

const result = await supertest.get(`${config.paths.pull}/${id}`)
expect(result.body).toEqual(expectedresult);
expect(result.statusCode).toEqual(200);

let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockResolvedValue({
toJson: () => Promise.resolve(expectedresult),
})

it('should return 404 if unable to find actor', async () => {

const id = faker.random.uuid(),

const result = await supertest.get(`${config.paths.pull}/${id}`)
expect(result.body).toEqual(expectedresult)
expect(result.statusCode).toEqual(200)
})

it('should return 404 if unable to find actor', async () => {
const id = faker.random.uuid(),
ActorSystemMock = {
actorFor: jest.fn()
actorFor: jest.fn(),
}

let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockRejectedValue("")

const result = await supertest.get(`${config.paths.pull}/${id}`)
let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockRejectedValue('')

expect(ActorSystemMock.actorFor).toHaveBeenCalledWith(id)
expect(result.statusCode).toEqual(404);
})
});
const result = await supertest.get(`${config.paths.pull}/${id}`)

expect(ActorSystemMock.actorFor).toHaveBeenCalledWith(id)
expect(result.statusCode).toEqual(404)
})
})

describe('push', () => {
it('should update state if actor exists', async () => {
const id = faker.random.uuid(),
it('should update state if actor exists', async () => {
const id = faker.random.uuid(),
body = { stuff: faker.random.uuid() },
ActorSystemMock = {
actorFor: jest.fn()
actorFor: jest.fn(),
},
ActorMock = {
updateFrom : jest.fn()
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)
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);
});
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 () => {
const id = faker.random.uuid(),
body = { stuff: faker.random.uuid(), type: "FakeActor" },
it('should crate actor and initialize state if actor does not exists', async () => {
const id = faker.random.uuid(),
body = { stuff: faker.random.uuid(), type: 'FakeActor' },
ActorSystemMock = {
actorFor: jest.fn(),
actorOf: jest.fn()
actorFor: jest.fn(),
actorOf: jest.fn(),
},
ActorMock = {
updateFrom : jest.fn()
updateFrom: jest.fn(),
}

let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockRejectedValue("")
ActorSystemMock.actorOf.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(ActorMock.updateFrom).toHaveBeenCalledWith(body)
expect(result.statusCode).toEqual(200);
});
});
let system: ActorSystem = jest.fn<ActorSystem>(() => ActorSystemMock)()
setupController(system)
ActorSystemMock.actorFor.mockRejectedValue('')
ActorSystemMock.actorOf.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(ActorMock.updateFrom).toHaveBeenCalledWith(body)
expect(result.statusCode).toEqual(200)
})
})
})

0 comments on commit 800b418

Please sign in to comment.