File size: 1,662 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import path from 'path'
import * as url from 'url'

import AdminJS from '../../adminjs.js'
import { ComponentLoader } from '../utils/index.js'
import generateUserComponentEntry from './generate-user-component-entry.js'

const __dirname = url.fileURLToPath(new URL('.', import.meta.url))

const exampleComponent = '../../../spec/fixtures/example-component.js'
const entryPath = './'

describe('generateUserComponentEntry', function () {
  it('defines AdminJS.UserComponents', function () {
    const adminJs = new AdminJS()

    const entryFile = generateUserComponentEntry(adminJs, entryPath)

    expect(entryFile).to.have.string('AdminJS.UserComponents = {}\n')
  })

  it('adds env variables to the entry file', function () {
    const adminJs = new AdminJS({
      env: { ENV_NAME: 'value' },
    })

    const entryFile = generateUserComponentEntry(adminJs, entryPath)

    expect(entryFile).to.have.string('AdminJS.env.ENV_NAME = "value"\n')
  })

  it('adds components to the entry file', function () {
    const loader = new ComponentLoader()
    const componentId = loader.add('ExampleComponent', exampleComponent)
    const adminJs = new AdminJS({ componentLoader: loader })
    const rootEntryPath = path.resolve(entryPath)
    const filePath = path.relative(
      rootEntryPath,
      path.normalize(path.join(__dirname, exampleComponent)),
    )

    const entryFile = generateUserComponentEntry(adminJs, entryPath)

    expect(entryFile).to.have.string([
      `import ${componentId} from '${filePath.replace('.js', '')}'`,
      `AdminJS.UserComponents.${componentId} = ${componentId}`,
    ].join('\n'))

    AdminJS.UserComponents = {}
  })
})