This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add *.spec.ts test files for Jest
- Loading branch information
Showing
10 changed files
with
1,276 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
Oops, something went wrong.