Skip to content

Commit

Permalink
Merge pull request #706 from retejs/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
Ni55aN authored Apr 20, 2024
2 parents 0414a24 + 56b3b0b commit 5859aa8
Show file tree
Hide file tree
Showing 10 changed files with 429 additions and 245 deletions.
68 changes: 0 additions & 68 deletions test/data/add-numbers.ts

This file was deleted.

31 changes: 0 additions & 31 deletions test/data/components.ts

This file was deleted.

122 changes: 0 additions & 122 deletions test/data/recursive.ts

This file was deleted.

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
}
Loading

0 comments on commit 5859aa8

Please sign in to comment.