Skip to content

Commit

Permalink
test: add tests for scope, utils and preset
Browse files Browse the repository at this point in the history
  • Loading branch information
Ni55aN committed Apr 20, 2024
1 parent 3960035 commit c0d43c6
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 1 deletion.
39 changes: 38 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// import jest globals
import { describe, expect, it } from '@jest/globals'

import { NodeEditor } from '../src/editor'
Expand Down Expand Up @@ -52,5 +51,43 @@ describe('NodeEditor', () => {

await expect(() => editor.addConnection(connectionData)).rejects.toThrowError()
})

it('removeNode should remove a node', async () => {
const editor = new NodeEditor()
const nodeData = { id: '1', label: 'Node 1' }

await editor.addNode(nodeData)
await editor.removeNode('1')
const nodes = editor.getNodes()

expect(nodes).toHaveLength(0)
})

it('removeConnection should remove a connection', async () => {
const editor = new NodeEditor()
const connectionData = { id: '1', source: '1', target: '2' }

await editor.addNode({ id: '1' })
await editor.addNode({ id: '2' })
await editor.addConnection(connectionData)
await editor.removeConnection('1')
const connections = editor.getConnections()

expect(connections).toHaveLength(0)
})

it('should clear all nodes and connections', async () => {
const editor = new NodeEditor()

await editor.addNode({ id: '1' })
await editor.addNode({ id: '2' })
await editor.addConnection({ id: '1', source: '1', target: '2' })
await editor.clear()
const nodes = editor.getNodes()
const connections = editor.getConnections()

expect(nodes).toHaveLength(0)
expect(connections).toHaveLength(0)
})
})

24 changes: 24 additions & 0 deletions test/mocks/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { jest } from '@jest/globals'
import { Buffer } from 'buffer'

export function mockCrypto(object: Record<string, unknown>) {
// eslint-disable-next-line no-undef
globalThis.crypto = object as unknown as Crypto
}

export function mockCryptoFromArray(array: Uint8Array) {
mockCrypto({
getRandomValues: jest.fn().mockReturnValue(array)
})
}

export function mockCryptoFromBuffer(buffer: Buffer) {
mockCrypto({
randomBytes: jest.fn().mockReturnValue(buffer)
})
}

export function resetCrypto() {
// eslint-disable-next-line no-undef, no-undefined
globalThis.crypto = undefined as unknown as Crypto
}
173 changes: 173 additions & 0 deletions test/presets/classic.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import { describe, expect, it } from '@jest/globals'

import { mockCryptoFromArray, resetCrypto } from '../mocks/crypto'

describe('ClassicPreset', () => {
// eslint-disable-next-line init-declarations
let preset!: typeof import('../../src/presets/classic')

beforeEach(async () => {
mockCryptoFromArray(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]))
preset = await import('../../src/presets/classic')
})

afterEach(() => {
resetCrypto()
})

describe('Node', () => {
it('is instantiable', () => {
expect(new preset.Node('A')).toBeInstanceOf(preset.Node)
})

it('should have an id', () => {
const node = new preset.Node('A')

expect(node.id).toBeDefined()
})

it('should have a label', () => {
const node = new preset.Node('A')

expect(node.label).toBe('A')
})

it('adds Input', () => {
const node = new preset.Node('A')
const input = new preset.Input(new preset.Socket('a'))

node.addInput('a', input)

expect(node.hasInput('a')).toBeTruthy()
expect(node.inputs['a']).toBe(input)
})

it('throws error if Input already exists', () => {
const node = new preset.Node('A')

node.addInput('a', new preset.Input(new preset.Socket('a')))

expect(() => node.addInput('a', new preset.Input(new preset.Socket('a')))).toThrow()
})

it('removes Input', () => {
const node = new preset.Node('A')

node.addInput('a', new preset.Input(new preset.Socket('a')))
node.removeInput('a')

expect(node.hasInput('a')).toBeFalsy()
})

it('adds Output', () => {
const node = new preset.Node('A')
const output = new preset.Output(new preset.Socket('a'))

node.addOutput('a', output)

expect(node.hasOutput('a')).toBeTruthy()
expect(node.outputs['a']).toBe(output)
})

it('throws error if Output already exists', () => {
const node = new preset.Node('A')

node.addOutput('a', new preset.Output(new preset.Socket('a')))

expect(() => node.addOutput('a', new preset.Output(new preset.Socket('a')))).toThrow()
})

it('removes Output', () => {
const node = new preset.Node('A')

node.addOutput('a', new preset.Output(new preset.Socket('a')))
node.removeOutput('a')

expect(node.hasOutput('a')).toBeFalsy()
})
})

describe('Connection', () => {
it('Connection throws error if input not found', () => {
const a = new preset.Node('A')
const b = new preset.Node('B')

a.addOutput('a', new preset.Output(new preset.Socket('a')))

expect(() => new preset.Connection(a, 'a', b, 'b')).toThrow()
})

it('Connection throws error if output not found', () => {
const a = new preset.Node('A')
const b = new preset.Node('B')

b.addInput('b', new preset.Input(new preset.Socket('b')))

expect(() => new preset.Connection(a, 'a', b, 'b')).toThrow()
})

it('Connection is instantiable', () => {
const a = new preset.Node('A')
const b = new preset.Node('B')
const output = new preset.Output(new preset.Socket('b'))
const input = new preset.Input(new preset.Socket('a'))

a.addOutput('a', output)
b.addInput('b', input)

expect(new preset.Connection(a, 'a', b, 'b')).toBeInstanceOf(preset.Connection)
})
})

describe('Control', () => {
it('adds Control to Node', () => {
const node = new preset.Node('A')

node.addControl('ctrl', new preset.Control())

expect(node.hasControl('ctrl')).toBeTruthy()
})

it('throws error if Control already exists', () => {
const node = new preset.Node('A')

node.addControl('ctrl', new preset.Control())

expect(() => node.addControl('ctrl', new preset.Control())).toThrow()
})

it('removes Control from Node', () => {
const node = new preset.Node('A')

node.addControl('ctrl', new preset.Control())
node.removeControl('ctrl')

expect(node.hasControl('ctrl')).toBeFalsy()
})

it('adds Control to Input', () => {
const input = new preset.Input(new preset.Socket('a'))

input.addControl(new preset.Control())

expect(input.control).toBeTruthy()
})

it('throws error if Control in Input already exists', () => {
const input = new preset.Input(new preset.Socket('a'))

input.addControl(new preset.Control())

expect(() => input.addControl(new preset.Control())).toThrow()
})

it('removes Control from Input', () => {
const input = new preset.Input(new preset.Socket('a'))

input.addControl(new preset.Control())
input.removeControl()

expect(input.control).toBeFalsy()
})
})
})
Loading

0 comments on commit c0d43c6

Please sign in to comment.