PHP 8.2.30
Preview: adminjs.spec.ts Size: 4.39 KB
/home/byroehnu/.trash/node_modules11/adminjs/src/adminjs.spec.ts

import path from 'path'
import { expect } from 'chai'
import * as url from 'url'

import AdminJS from './adminjs.js'
import BaseDatabase from './backend/adapters/database/base-database.js'
import BaseResource from './backend/adapters/resource/base-resource.js'
import { ComponentLoader } from './backend/utils/component-loader.js'

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

describe('AdminJS', function () {
  beforeEach(function () {
    global.RegisteredAdapters = []
  })

  describe('#constructor', function () {
    it('sets default root path when no given', function () {
      expect(new AdminJS().options.rootPath).to.equal('/admin')
    })
  })

  describe('.AdminJS.registerAdapter', function () {
    beforeEach(function () {
      class Database extends BaseDatabase {}
      class Resource extends BaseResource {}
      this.DatabaseAdapter = { Database, Resource }
    })

    it('adds given adapter to list off all available adapters', function () {
      AdminJS.registerAdapter(this.DatabaseAdapter)
      expect(global.RegisteredAdapters).to.have.lengthOf(1)
    })

    it('throws an error when adapter is not full', function () {
      expect(() => {
        AdminJS.registerAdapter({
          Resource: BaseResource,
          Database: null as unknown as typeof BaseDatabase })
      }).to.throw('Adapter has to have both Database and Resource')
    })

    it('throws an error when adapter has elements not being subclassed from base adapter', function () {
      expect(() => {
        AdminJS.registerAdapter({
          Resource: {} as typeof BaseResource,
          Database: {} as typeof BaseDatabase,
        })
      }).to.throw('Adapter elements have to be a subclass of AdminJS.BaseResource and AdminJS.BaseDatabase')
    })
  })

  describe('resolveBabelConfigPath', function () {
    it('load .babelrc file', function () {
      const adminJS = new AdminJS({ bundler: { babelConfig: '../.babelrc.json' } })
      expect(adminJS.options.bundler.babelConfig).not.to.undefined
    })

    it('load with json object directly', function () {
      const adminJS = new AdminJS({ bundler: { babelConfig: {
        presets: [
          '@babel/preset-react',
          ['@babel/preset-env', {
            targets: {
              node: '18',
            },
            modules: false,
            loose: true,
          }],
          '@babel/preset-typescript',
        ],
        plugins: ['@babel/plugin-syntax-import-assertions'],
        only: ['src/', 'spec/'],
        ignore: [
          'src/frontend/assets/scripts/app-bundle.development.js',
          'src/frontend/assets/scripts/app-bundle.production.js',
          'src/frontend/assets/scripts/global-bundle.development.js',
          'src/frontend/assets/scripts/global-bundle.production.js',
        ],
      } } })
      expect(adminJS.options.bundler.babelConfig).not.to.undefined
    })

    it('load babel.config.cjs file', function () {
      const adminJS = new AdminJS({ bundler: { babelConfig: './babel.test.config.json' } })
      expect(adminJS.options.bundler.babelConfig).not.to.undefined
    })
  })

  describe('ComponentLoader', function () {
    const loader = new ComponentLoader()
    afterEach(function () {
      loader.clear()
    })
    context('file exists', function () {
      beforeEach(function () {
        this.result = loader.add('ExampleComponent', '../spec/fixtures/example-component')
      })

      it('adds given file to a UserComponents object', function () {
        expect(Object.keys(loader.getComponents())).to.have.lengthOf(1)
      })

      it('returns uniqe id', function () {
        expect(loader.getComponents()[this.result]).not.to.be.undefined
        expect(this.result).to.be.a('string')
      })

      it('converts relative path to absolute path', function () {
        expect(
          loader.getComponents()[this.result],
        ).to.equal(path.join(__dirname, '../spec/fixtures/example-component'))
      })
    })

    context('component name given', function () {
      it('returns the same component name as which was given', function () {
        const name = loader.add('Dashboard', '../spec/fixtures/example-component')
        expect(name).to.eq('Dashboard')
      })
    })

    it('throws an error when component doesn\'t exist', function () {
      expect(() => {
        loader.add('ExampleComponent', './fixtures/example-components')
      }).to.throw().property('name', 'ConfigurationError')
    })
  })
})

Directory Contents

Dirs: 4 × Files: 8

Name Size Perms Modified Actions
backend DIR
- drwxr-xr-x 2026-02-28 00:32:28
Edit Download
frontend DIR
- drwxr-xr-x 2026-02-28 00:32:48
Edit Download
locale DIR
- drwxr-xr-x 2026-02-28 00:32:28
Edit Download
utils DIR
- drwxr-xr-x 2026-02-28 00:32:46
Edit Download
11.92 KB lrw-r--r-- 2026-02-28 00:31:58
Edit Download
4.39 KB lrw-r--r-- 2026-02-28 00:31:58
Edit Download
10.14 KB lrw-r--r-- 2026-02-28 00:32:00
Edit Download
592 B lrw-r--r-- 2026-02-28 00:31:52
Edit Download
227 B lrw-r--r-- 2026-02-28 00:32:02
Edit Download
915 B lrw-r--r-- 2026-02-28 00:32:04
Edit Download
789 B lrw-r--r-- 2026-02-28 00:32:04
Edit Download
321 B lrw-r--r-- 2026-02-28 00:32:28
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).