Skip to content
Snippets Groups Projects
  • david.bauer's avatar
    f03f2f5d
    Refactoring: Separated fastify plugins and routes · f03f2f5d
    david.bauer authored
    - Unit and Integration tests now use "inject" instead of starting a server
      and using supertest.
    - Add Coverage Reports and "test:dev" mode. This enables a mocha --watch
      like workflow using nodemon, as --watch does not work with esm modules.
    - Reorganized and renamed files and functions
    f03f2f5d
    History
    Refactoring: Separated fastify plugins and routes
    david.bauer authored
    - Unit and Integration tests now use "inject" instead of starting a server
      and using supertest.
    - Add Coverage Reports and "test:dev" mode. This enables a mocha --watch
      like workflow using nodemon, as --watch does not work with esm modules.
    - Reorganized and renamed files and functions
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
server_test.js 2.68 KiB
import { decompressBrotli, generateSimpleViteManifest, injectApp, mockConfig, mockFetch, mockRedis, wait } from './util.js'
import { expect } from 'chai'
import * as td from 'testdouble'
import RedisMock from 'ioredis-mock'

describe('UI Middleware', function () {
  let app
  let fetchConfig

  beforeEach(async function () {
    mockConfig({ baseUrls: ['http://ui-server/'] })
    mockRedis()
    mockFetch(fetchConfig = {
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({ 'example.js': 'test' }),
        '/example.js': ''
      }
    })
    app = await injectApp()
  })

  afterEach(async function () {
    td.reset()
    await new RedisMock().flushdb()
  })

  it('fetches manifest data', async function () {
    const response = await app.inject({ url: '/manifests' })
    const body = await decompressBrotli(response.rawPayload)
    expect(response.statusCode).to.equal(200)
    expect(response.headers['content-encoding']).to.equal('br')
    expect(body).to.deep.equal([{ namespace: 'test', path: 'example' }])
  })

  it('caches manifest data when configuration changes', async function () {
    const response = await app.inject({ url: '/manifests' })
    const body = await decompressBrotli(response.rawPayload)
    expect(response.statusCode).to.equal(200)
    expect(response.headers['content-encoding']).to.equal('br')
    expect(body).to.deep.equal([{ namespace: 'test', path: 'example' }])

    fetchConfig['http://ui-server'] = {
      '/manifest.json': generateSimpleViteManifest({ 'example.js': 'other' }),
      '/example.js': ''
    }

    await wait(150)

    const response2 = await app.inject({ url: '/manifests' })
    const body2 = await decompressBrotli(response.rawPayload)
    expect(response2.statusCode).to.equal(200)
    expect(response2.headers['content-encoding']).to.equal('br')
    expect(body2).to.deep.equal([{ namespace: 'test', path: 'example' }])
  })

  describe('multiple configurations', function () {
    beforeEach(async function () {
      mockConfig({ baseUrls: ['http://ui-server/', 'http://ui-server2/'] })
      fetchConfig['http://ui-server2'] = {
        '/manifest.json': generateSimpleViteManifest({ 'example2.js': 'thing' }),
        '/example2.js': ''
      }
      app = await injectApp()
    })

    it('can load multiple configurations', async function () {
      const response = await app.inject({ url: '/manifests' })
      const body = await decompressBrotli(response.rawPayload)
      expect(response.statusCode).to.equal(200)
      expect(response.headers['content-encoding']).to.equal('br')
      expect(body).to.deep.equal([
        { namespace: 'test', path: 'example' },
        { namespace: 'thing', path: 'example2' }
      ])
    })
  })
})