Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
file-depencies_test.js 3.13 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('JS files with dependencies contain events', function () {
  let app
  let mockFetchConfig

  beforeEach(async function () {
    mockConfig({ baseUrls: ['http://ui-server/'] })
    mockRedis()
    mockFetch(mockFetchConfig = {
      'http://ui-server': {
        '/manifest.json': generateSimpleViteManifest({
          'example.js': {},
          'main.css': {},
          'index.html': {
            file: 'index.html.js',
            isEntry: true,
            imports: ['example.js'],
            css: ['main.css']
          }
        }),
        '/example.js': () => new Response('this is example', { headers: { 'content-type': 'application/javascript' } }),
        '/index.html.js': () => new Response('console.log("this is index.html.js")', { headers: { 'content-type': 'application/javascript' } }),
        '/index.html': () => new Response('<html><head></head><body>it\'s me</body></html>', { headers: { 'content-type': 'text/html' } }),
        '/main.css': () => new Response('.foo { color: #000; }', { headers: { 'content-type': 'text/css' } })
      }
    })
    app = await mockApp()
  })

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

  it('javascript file contains dispatcher for dependencies', async function () {
    const response = await request(app.server).get('/index.html.js')
    expect(response.statusCode).to.equal(200)
    expect(response.headers.dependencies).to.equal('main.css')
    expect(response.text).to.equal('console.log("this is index.html.js")\n/*injected by ui-middleware*/document.dispatchEvent(new CustomEvent("load-css",{detail:{css:["main.css"]}}))')
  })

  it('javascript files from different versions have correct dependencies', async function () {
    const r1 = await request(app.server).get('/index.html.js')
    expect(r1.headers.dependencies).to.equal('main.css')

    mockFetchConfig['http://ui-server']['/manifest.json'] = generateSimpleViteManifest({
      'example.js': {},
      'other.css': {},
      'index.html': {
        file: 'index.html.js',
        isEntry: true,
        imports: ['example.js'],
        css: ['other.css']
      }
    })

    await import('../src/version.js').then(async ({ updateVersionProcessor }) => {
      // need to process two times to actually trigger the update
      await updateVersionProcessor()
      await updateVersionProcessor()
    })

    const r2 = await request(app.server).get('/index.html.js')
    expect(r2.headers.dependencies).to.equal('other.css')

    const r3 = await request(app.server).get('/index.html.js').set('version', r1.headers.version)
    expect(r3.headers.dependencies).to.equal('main.css')

    const r4 = await request(app.server).get('/index.html.js').set('version', r2.headers.version)
    expect(r4.headers.dependencies).to.equal('other.css')

    const r5 = await request(app.server).get('/index.html.js')
    expect(r5.headers.dependencies).to.equal('other.css')
  })
})