summaryrefslogtreecommitdiff
path: root/server/src/server.test.ts
blob: 2d0efcab0d30e504b6dbef90d9840b4dcc818d91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { runServer } from './server'
import process from 'process'

// We test the server using the client.
// It would be better to import this
// from a linked npm package,
// but that requires rebuilding each time
// so fuck it for now
import { dbFactory, DatabaseConnection } from '../../client/src'

const port = +(process.env.PORT || 5005)
const httpPort = port
const wsPort = port + 1
const localHost = 'http://localhost:'

describe('Server module', async () => {
  let db: DatabaseConnection
  let cleanup: () => Promise<void>
  beforeAll(async () => {
    cleanup = await runServer({
      httpPort,
      wsPort,
      logger: console.log
    })
    db = dbFactory({
      wsUrl: `${localHost}${wsPort}`,
      httpUrl: `${localHost}${httpPort}`
    })
    await db.init()
  })
  afterAll(async () => {
    if (cleanup) {
      await cleanup()
    }
  })
  test('it should leave input data unchaged', async () => {
    const path = '/hello/world'
    const fixture = {
      my: 'data'
    }
    await db.write(path, fixture)
    const r = await db.read(path)
    expect(r).toMatchObject(fixture)
  })
  test('it should get data in realtime', () => {
    return new Promise(async (resolve, reject) => {
      db.subscribe('/hello/world', (data: any) => {
        resolve()
      })
      await db.write('/hello/world', { data: 5 })
    })
  })
})