Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
server_test.js 3.20 KiB
import request from 'supertest'
import { 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({ urls: ['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()
    process.env.CACHE_TTL = '30000'
  })

  // Some say, this is not necessary to test
  // But failing startups due to code errors in the probes will cause this to not work
  // Therefore, we should keep this
  it('is ready', async function () {
    const response = await request(app.server).get('/ready')
    expect(response.statusCode).to.equal(200)
  })

  it('is live', async function () {
    const response = await request(app.server).get('/live')
    expect(response.statusCode).to.equal(200)
  })

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

  describe('when configuration changes', function () {
    let prevConfig
    beforeEach(async function () {
      prevConfig = fetchConfig['http://ui-server']
    })

    afterEach(function () {
      fetchConfig['http://ui-server'] = prevConfig
    })

    it('caches manifest data', async function () {
      const response = await request(app.server).get('/manifests')
      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')
      expect(response2.statusCode).to.equal(200)
      expect(response2.body).to.deep.equal([{ namespace: 'test', path: 'example' }])
    })
  })

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

    afterEach(function () {
      delete fetchConfig['http://ui-server2']
      app = prevApp
    })

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