Skip to content

Commit

Permalink
test(plugin-adapter): add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DerYeger committed Nov 5, 2024
1 parent 74b7c2d commit 3dcede0
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/framework/plugin-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"check:publish": "publint run --strict",
"check:tsc": "tsc",
"dev": "vite build --watch",
"lint": "yeger-lint"
"lint": "yeger-lint",
"test": "vitest"
},
"dependencies": {
"@cm2ml/deduplicate": "workspace:*",
Expand Down
101 changes: 101 additions & 0 deletions packages/framework/plugin-adapter/test/plugin-adapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import type { Plugin } from '@cm2ml/plugin'
import { definePlugin } from '@cm2ml/plugin'
import { describe, expect, it, vi } from 'vitest'

import { PluginAdapter } from '../src'

const testPluginA = definePlugin({
name: 'a',
parameters: {},
invoke(input: string) {
return { data: input, metadata: 'a' }
},
})

const testPluginB = definePlugin({
name: 'b',
parameters: {},
invoke(input: string) {
return { data: input, metadata: 'b' }
},
})

class TestPluginAdapter extends PluginAdapter<string, unknown> {
public readonly onApplySpy = vi.fn()
public readonly onStartSpy = vi.fn()

protected onApply(plugin: Plugin<string, string, any>) {
this.onApplySpy(plugin)
}

protected onStart() {
this.onStartSpy(this.state)
}

public getInternalState() {
return this.state
}

public getInternalPlugins() {
return this.plugins
}
}

describe('plugin adapter', () => {
describe('application', () => {
it('can apply a single plugin', () => {
const adapter = new TestPluginAdapter()
adapter.apply(testPluginA)
expect(adapter.getInternalPlugins().size).toBe(1)
expect(adapter.getInternalPlugins().get('a')).toBe(testPluginA)
})

it('cannot apply a plugin twice', () => {
const adapter = new TestPluginAdapter()
adapter.apply(testPluginA)
expect(() => adapter.apply(testPluginA)).toThrowErrorMatchingInlineSnapshot(`[Error: Plugin a already applied.]`)
})

it('calls onApply for each plugin', () => {
const adapter = new TestPluginAdapter()
adapter.applyAll([testPluginA, testPluginB])
expect(adapter.onApplySpy).toHaveBeenCalledTimes(2)
expect(adapter.onApplySpy).toHaveBeenCalledWith(testPluginA)
expect(adapter.onApplySpy).toHaveBeenCalledWith(testPluginB)
expect(adapter.getInternalPlugins().size).toBe(2)
expect(adapter.getInternalPlugins().get('a')).toBe(testPluginA)
expect(adapter.getInternalPlugins().get('b')).toBe(testPluginB)
})

it('cannot apply plugins unless in not-started state', () => {
const adapter = new TestPluginAdapter()
adapter.start()
expect(() => adapter.apply(testPluginA)).toThrowErrorMatchingInlineSnapshot(`[Error: PluginAdapter has already been started.]`)
})
})

describe('state', () => {
it('is not started initially', () => {
const adapter = new TestPluginAdapter()
expect(adapter.getInternalState()).toBe('not-started')
})

it('it calls onStart while starting', () => {
const adapter = new TestPluginAdapter()
adapter.start()
expect(adapter.onStartSpy).toHaveBeenCalledWith('starting')
})

it('is started after onStart is called', () => {
const adapter = new TestPluginAdapter()
adapter.start()
expect(adapter.getInternalState()).toBe('started')
})

it('throws when start is called twice', () => {
const adapter = new TestPluginAdapter()
adapter.start()
expect(() => adapter.start()).toThrow()
})
})
})
3 changes: 3 additions & 0 deletions packages/tsconfig/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @cm2ml/tsconfig

This package provides a set of TypeScript configurations for different scenarios.

0 comments on commit 3dcede0

Please sign in to comment.