Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
server_test.js 2.49 KiB
import request from 'supertest'
import { brotliParser, generateSimpleViteManifest, mockApp, mockConfig, mockFetch, mockRedis } 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 mockApp()
  })

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

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

  it('caches manifest data when configuration changes', async function () {
    const response = await request(app.server).get('/manifests').parse(brotliParser)
    expect(response.statusCode).to.equal(200)
    expect(response.body).to.deep.equal([{ namespace: 'test', path: 'example' }])

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

    await new Promise(resolve => setTimeout(resolve, 150))

    const response2 = await request(app.server).get('/manifests').parse(brotliParser)
    expect(response2.statusCode).to.equal(200)
    expect(response2.body).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 mockApp()
    })

    it('can load multiple configurations', async function () {
      await request(app.server)
        .get('/manifests')
        .parse(brotliParser)
        .then(response => {
          expect(response.statusCode).to.equal(200)
          expect(response.body).to.deep.equal([
            { namespace: 'test', path: 'example' },
            { namespace: 'thing', path: 'example2' }
          ])
        })
    })
  })
})