Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
test: add *.spec.ts test files for Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
drichar committed Dec 9, 2023
1 parent 7172ee4 commit 86952e9
Show file tree
Hide file tree
Showing 10 changed files with 1,276 additions and 5 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:

- name: Set up Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.15

- name: Install dependencies
run: bun install
Expand All @@ -34,11 +36,11 @@ jobs:
- name: Typecheck
run: bun run typecheck

- name: Run tests
run: bun test
- name: Run tests (Bun)
run: bun run test:bun

# - name: Run Jest tests
# run: bun run test
- name: Run tests (Jest)
run: bun run test:jest

- name: Build
run: bun run build
84 changes: 84 additions & 0 deletions __tests__/network/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { describe, expect, it } from '@jest/globals'
import { NetworkId } from 'src/network/constants'
import { isAlgodConfig, isNetworkConfigMap, isValidNetworkId } from 'src/network/utils'

describe('Type Guards', () => {
describe('isValidNetworkId', () => {
it('returns true for a valid NetworkId', () => {
expect(isValidNetworkId(NetworkId.TESTNET)).toBe(true)
})

it('returns false for an invalid NetworkId', () => {
expect(isValidNetworkId('foo')).toBe(false)
})
})

describe('isAlgodConfig', () => {
it('returns true for a valid AlgodConfig', () => {
expect(
isAlgodConfig({
token: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
baseServer: 'http://localhost',
port: 1234,
headers: {
'X-Foo': 'bar'
}
})
).toBe(true)

expect(
isAlgodConfig({
token: '',
baseServer: ''
})
).toBe(true)
})

it('returns false for an invalid AlgodConfig', () => {
expect(
isAlgodConfig({
baseServer: ''
})
).toBe(false)

expect(
isAlgodConfig({
token: ''
})
).toBe(false)

expect(
isAlgodConfig({
token: '',
baseServer: '',
foo: ''
})
).toBe(false)
})
})

describe('isNetworkConfigMap', () => {
it('returns true for a valid NetworkConfigMap', () => {
const validConfigMap = {
[NetworkId.MAINNET]: {
token: '',
baseServer: ''
},
[NetworkId.TESTNET]: {
token: '',
baseServer: ''
}
}
expect(isNetworkConfigMap(validConfigMap)).toBe(true)
})

it('returns false for an invalid NetworkConfigMap', () => {
expect(
isNetworkConfigMap({
token: '',
baseServer: ''
})
).toBe(false)
})
})
})
50 changes: 50 additions & 0 deletions __tests__/store/pubsub.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { beforeEach, describe, expect, it, jest } from '@jest/globals'
import { PubSub } from 'src/store/pubsub'

describe('PubSub', () => {
let pubSub: PubSub

beforeEach(() => {
pubSub = new PubSub()
})

it('should call all callbacks subscribed to an event when it is published', () => {
const callback1 = jest.fn()
const callback2 = jest.fn()
const event = 'testEvent'
const data = { key: 'value' }

pubSub.subscribe(event, callback1)
pubSub.subscribe(event, callback2)
pubSub.publish(event, data)

expect(callback1).toHaveBeenCalledWith(data)
expect(callback2).toHaveBeenCalledWith(data)
})

it('should not call callbacks subscribed to different events', () => {
const callback1 = jest.fn()
const event1 = 'testEvent1'
const event2 = 'testEvent2'
const data = { key: 'value' }

pubSub.subscribe(event1, callback1)
pubSub.publish(event2, data)

expect(callback1).not.toHaveBeenCalled()
})

it('should pass the correct data to callbacks when an event is published', () => {
const callback = jest.fn()
const event = 'testEvent'
const data1 = { key: 'value1' }
const data2 = { key: 'value2' }

pubSub.subscribe(event, callback)
pubSub.publish(event, data1)
pubSub.publish(event, data2)

expect(callback).toHaveBeenNthCalledWith(1, data1)
expect(callback).toHaveBeenNthCalledWith(2, data2)
})
})
Loading

0 comments on commit 86952e9

Please sign in to comment.