-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(loader): add more loader tests, fix constructor detection
- Loading branch information
Showing
4 changed files
with
144 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,52 @@ | ||
import { Dict } from 'cosmokit' | ||
import { Context, Plugin } from '@cordisjs/core' | ||
import { group, Loader } from '../src/shared' | ||
import { Entry, group, Loader } from '../src' | ||
import { Mock, mock } from 'node:test' | ||
import { expect } from 'chai' | ||
|
||
declare module '../src/shared' { | ||
interface Loader { | ||
register(name: string, plugin: any): void | ||
mock<F extends Function>(name: string, plugin: F): Mock<F> | ||
restart(config: Entry.Options[]): Promise<void> | ||
expectEnable(plugin: any, config?: any): void | ||
expectDisable(plugin: any): void | ||
} | ||
} | ||
|
||
export default class MockLoader extends Loader { | ||
public data: Dict<Plugin.Object> = Object.create(null) | ||
public modules: Dict<Plugin.Object> = Object.create(null) | ||
|
||
constructor(ctx: Context) { | ||
super(ctx, { name: 'cordis' }) | ||
this.register('cordis/group', group) | ||
this.mock('cordis/group', group) | ||
this.writable = true | ||
} | ||
|
||
register(name: string, plugin: any) { | ||
this.data[name] = plugin | ||
mock<F extends Function>(name: string, plugin: F) { | ||
return this.modules[name] = mock.fn(plugin) | ||
} | ||
|
||
async import(name: string) { | ||
return this.data[name] | ||
return this.modules[name] | ||
} | ||
|
||
async readConfig() { | ||
return this.config | ||
} | ||
|
||
async restart(config: Entry.Options[]) { | ||
this.config = config | ||
return this.start() | ||
} | ||
|
||
expectEnable(plugin: any, config?: any) { | ||
const runtime = this.ctx.registry.get(plugin) | ||
expect(runtime).to.be.ok | ||
expect(runtime!.config).to.deep.equal(config) | ||
} | ||
|
||
expectDisable(plugin: any) { | ||
const runtime = this.ctx.registry.get(plugin) | ||
expect(runtime).to.be.not.ok | ||
} | ||
} |