This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
bug fixes and unit tests #14
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
Empty file.
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,115 @@ | ||
import { EoswsClient } from "../eosws-client" | ||
import { InboundMessage } from "../inbound" | ||
import { | ||
GetActionTracesMessageBackendParameters, | ||
OutboundMessage, | ||
OutboundMessageType | ||
} from "../outbound" | ||
|
||
describe("EoswsClient", function() { | ||
let controller: ReturnType<typeof createSocketController> | ||
let factory: () => WebSocket | ||
let receivedMessages: Array<InboundMessage<any>> | ||
|
||
beforeEach(() => { | ||
controller = createSocketController() | ||
factory = () => controller as any | ||
receivedMessages = [] | ||
}) | ||
|
||
describe("constructor", () => { | ||
it("should build the client", () => { | ||
const client = new EoswsClient(factory) | ||
expect(client.client).toBeDefined() | ||
}) | ||
}) | ||
|
||
describe("connect", () => { | ||
it("should add a listener", () => { | ||
const client = new EoswsClient(factory) | ||
spyOn(client.client, "connect") | ||
client.connect() | ||
expect(client.client.connect).toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe("send", () => { | ||
it("return a listener if listen is set to true", () => { | ||
const client = new EoswsClient(factory) | ||
expect( | ||
client.send<OutboundMessage<GetActionTracesMessageBackendParameters>>(basicGetActionMessage) | ||
).toEqual({ requestId: "abc", listen: expect.any(Function), unlisten: expect.any(Function) }) | ||
}) | ||
|
||
it("should return null if listen is set to false", () => { | ||
const client = new EoswsClient(factory) | ||
expect( | ||
client.send<OutboundMessage<GetActionTracesMessageBackendParameters>>( | ||
Object.assign({}, basicGetActionMessage, { listen: false }) | ||
) | ||
).toEqual(null) | ||
}) | ||
}) | ||
|
||
describe("getActionTraces", () => { | ||
it("should return the same object as the basic send", () => { | ||
const client = new EoswsClient(factory) | ||
|
||
expect( | ||
client.getActionTraces( | ||
{ receiver: "test", account: "account_name", actionName: "test" }, | ||
{ listen: true, requestId: "abc" } | ||
) | ||
).toEqual({ requestId: "abc", listen: expect.any(Function), unlisten: expect.any(Function) }) | ||
}) | ||
}) | ||
|
||
describe("getTableRows", () => { | ||
it("should return the same object as the basic send", () => { | ||
const client = new EoswsClient(factory) | ||
|
||
expect( | ||
client.getTableRows( | ||
{ scope: "test", code: "code", tableName: "test" }, | ||
{ listen: true, requestId: "abc" } | ||
) | ||
).toEqual({ requestId: "abc", listen: expect.any(Function), unlisten: expect.any(Function) }) | ||
}) | ||
}) | ||
|
||
describe("getTransaction", () => { | ||
it("should return the same object as the basic send", () => { | ||
const client = new EoswsClient(factory) | ||
|
||
expect(client.getTransaction("id", { listen: true, requestId: "abc" })).toEqual({ | ||
requestId: "abc", | ||
listen: expect.any(Function), | ||
unlisten: expect.any(Function) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
const basicGetActionMessage = { | ||
type: OutboundMessageType.GET_ACTION_TRACES, | ||
listen: true, | ||
req_id: "abc", | ||
data: { receiver: "test", account: "account_name", action_name: "test" } | ||
} | ||
|
||
interface WebSocketController { | ||
close: jest.Mock<() => void> | ||
send: jest.Mock<(data: any) => void> | ||
|
||
onclose?: (event: CloseEvent) => void | ||
onerror?: (event: Event) => void | ||
onopen?: () => void | ||
onmessage?: (event: MessageEvent) => void | ||
} | ||
|
||
function createSocketController(): WebSocketController { | ||
return { | ||
close: jest.fn<() => void>(), | ||
send: jest.fn<(data: any) => void>() | ||
} | ||
} |
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,90 @@ | ||
import { EoswsListeners, ListenerObject } from "../eosws-listeners" | ||
import { InboundMessageType } from "../inbound" | ||
|
||
describe("EoswsListeners", function() { | ||
describe("addListener", () => { | ||
it("should add a listener to the list", () => { | ||
const listenerObject: ListenerObject = { | ||
requestId: "abc", | ||
messageTypes: [InboundMessageType.TABLE_DELTA], | ||
callback: () => { | ||
console.log("test") | ||
} | ||
} | ||
|
||
const listeners = new EoswsListeners() | ||
|
||
listeners.addListener(listenerObject) | ||
|
||
expect(listeners.registeredListeners).toEqual([listenerObject]) | ||
}) | ||
}) | ||
|
||
describe("removeListener", () => { | ||
it("should add a listener to the list", () => { | ||
const listenerObject1: ListenerObject = { | ||
requestId: "abc", | ||
messageTypes: [InboundMessageType.TABLE_DELTA], | ||
callback: () => { | ||
console.log("test") | ||
} | ||
} | ||
|
||
const listenerObject2: ListenerObject = { | ||
requestId: "abcd", | ||
messageTypes: [InboundMessageType.TABLE_DELTA], | ||
callback: () => { | ||
console.log("test") | ||
} | ||
} | ||
|
||
const listeners = new EoswsListeners() | ||
|
||
listeners.addListener(listenerObject1) | ||
listeners.addListener(listenerObject2) | ||
expect(listeners.registeredListeners).toEqual([listenerObject1, listenerObject2]) | ||
|
||
listeners.removeListener("abc") | ||
|
||
expect(listeners.registeredListeners).toEqual([listenerObject2]) | ||
}) | ||
}) | ||
|
||
describe("handleMessage", () => { | ||
it("should process the callback given the right id and type", () => { | ||
const listenerObject1: ListenerObject = { | ||
requestId: "abc", | ||
messageTypes: [InboundMessageType.TABLE_DELTA], | ||
callback: () => { | ||
console.log("test") | ||
} | ||
} | ||
|
||
spyOn(listenerObject1, "callback") | ||
|
||
const listenerObject2: ListenerObject = { | ||
requestId: "abcd", | ||
messageTypes: [InboundMessageType.TABLE_DELTA], | ||
callback: () => { | ||
console.log("test") | ||
} | ||
} | ||
const listeners = new EoswsListeners() | ||
|
||
listeners.addListener(listenerObject1) | ||
listeners.addListener(listenerObject2) | ||
|
||
listeners.handleMessage(InboundMessageType.TABLE_DELTA, { | ||
type: InboundMessageType.TABLE_DELTA, | ||
req_id: "abc", | ||
data: { test: "foo" } | ||
}) | ||
|
||
expect(listenerObject1.callback).toHaveBeenCalledWith(InboundMessageType.TABLE_DELTA, { | ||
type: InboundMessageType.TABLE_DELTA, | ||
req_id: "abc", | ||
data: { test: "foo" } | ||
}) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to have
{ listen: true }
has the default forgetActionTraces
. In fact, later on, we should even not permit to not uselisten
on some of the callbacks (likegetActionTraces
).