Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #706

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"devDependencies": {
"jest-environment-jsdom": "^29.1.2",
"rete-cli": "^1.0.1",
"rete-cli": "^1.0.2",
"typescript": "4.8.4"
},
"dependencies": {
Expand Down
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
Loading