|
| 1 | +import AblyLink from "../AblyLink" |
| 2 | +import { Realtime } from "ably" |
| 3 | +import { Operation } from "@apollo/client/core" |
| 4 | +import { parse } from "graphql" |
| 5 | +function createAbly() { |
| 6 | + const _channels: {[key: string]: any } = {} |
| 7 | + const log: any[] = [] |
| 8 | + |
| 9 | + const ably = { |
| 10 | + _channels: _channels, |
| 11 | + log: log, |
| 12 | + auth: { |
| 13 | + clientId: null, |
| 14 | + }, |
| 15 | + channels: { |
| 16 | + get(channelName: string) { |
| 17 | + return _channels[channelName] ||= { |
| 18 | + _listeners: [] as [string, Function][], |
| 19 | + name: channelName, |
| 20 | + presence: { |
| 21 | + enterClient(_clientName: string, _status: string) {}, |
| 22 | + leaveClient(_clientName: string) {}, |
| 23 | + }, |
| 24 | + detach(callback: Function) { |
| 25 | + callback() |
| 26 | + }, |
| 27 | + subscribe(eventName: string, callback: Function) { |
| 28 | + log.push(["subscribe", channelName, eventName]) |
| 29 | + this._listeners.push([eventName, callback]) |
| 30 | + }, |
| 31 | + unsubscribe(){ |
| 32 | + log.push(["unsubscribe", channelName]) |
| 33 | + } |
| 34 | + } |
| 35 | + }, |
| 36 | + release(channelName: string) { |
| 37 | + delete _channels[channelName] |
| 38 | + } |
| 39 | + }, |
| 40 | + __testTrigger(channelName: string, eventName: string, data: any) { |
| 41 | + const channel = this.channels.get(channelName) |
| 42 | + const handler = channel._listeners.find((l: any) => l[0] == eventName) |
| 43 | + if (handler) { |
| 44 | + handler[1](data) |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return (ably as unknown) as Realtime |
| 50 | +} |
| 51 | + |
| 52 | +function createOperation(options: { subscriptionId: string | null }) { |
| 53 | + return ({ |
| 54 | + query: parse("subscription { foo { bar } }"), |
| 55 | + variables: { a: 1 }, |
| 56 | + operationId: "operationId", |
| 57 | + operationName: "operationName", |
| 58 | + getContext: () => { |
| 59 | + return { |
| 60 | + response: { |
| 61 | + headers: { |
| 62 | + get: (key: string) => { |
| 63 | + if (key == "X-Subscription-ID") { |
| 64 | + return options.subscriptionId |
| 65 | + } else { |
| 66 | + return null |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } as unknown) as Operation |
| 74 | +} |
| 75 | +describe("AblyLink", () => { |
| 76 | + test("delegates to Ably", () => { |
| 77 | + var mockAbly = createAbly() |
| 78 | + var log = (mockAbly as any).log |
| 79 | + var operation = createOperation({subscriptionId: "sub-1234"}) |
| 80 | + |
| 81 | + var nextLink = (operation: any) => { |
| 82 | + log.push(["forward", operation.operationName]) |
| 83 | + return { |
| 84 | + subscribe(info: any) { |
| 85 | + info.next() |
| 86 | + } |
| 87 | + } as any |
| 88 | + } |
| 89 | + |
| 90 | + var observable = new AblyLink({ ably: mockAbly}).request(operation, nextLink) |
| 91 | + |
| 92 | + observable.subscribe(function(result: any) { |
| 93 | + log.push(["received", result]) |
| 94 | + }); |
| 95 | + |
| 96 | + (mockAbly as any).__testTrigger("sub-1234", "update", { data: { result: { data: null }, more: true} }); |
| 97 | + (mockAbly as any).__testTrigger("sub-1234", "update", { data: { result: { data: "data 1" }, more: true} }); |
| 98 | + (mockAbly as any).__testTrigger("sub-1234", "update", { data: { result: { data: "data 2" }, more: false} }); |
| 99 | + |
| 100 | + expect(log).toEqual([ |
| 101 | + ["forward", "operationName"], |
| 102 | + ["subscribe", "sub-1234", "update"], |
| 103 | + ["received", { data: null }], |
| 104 | + ["received", { data: "data 1" }], |
| 105 | + ["received", { data: "data 2" }], |
| 106 | + ["unsubscribe", "sub-1234"] |
| 107 | + ]) |
| 108 | + }) |
| 109 | + |
| 110 | + test("it doesn't call ably when the subscription header isn't present", () => { |
| 111 | + var mockAbly = createAbly() |
| 112 | + var log = (mockAbly as any).log |
| 113 | + var operation = createOperation({subscriptionId: null}) |
| 114 | + |
| 115 | + var nextLink = (operation: any) => { |
| 116 | + log.push(["forward", operation.operationName]) |
| 117 | + return { |
| 118 | + subscribe(info: any) { |
| 119 | + info.next() |
| 120 | + } |
| 121 | + } as any |
| 122 | + } |
| 123 | + |
| 124 | + var observable = new AblyLink({ ably: mockAbly}).request(operation, nextLink) |
| 125 | + |
| 126 | + observable.subscribe(function(result: any) { |
| 127 | + log.push(["received", result]) |
| 128 | + }); |
| 129 | + |
| 130 | + (mockAbly as any).__testTrigger("sub-1234", "update", { data: { result: { data: null }, more: true} }); |
| 131 | + (mockAbly as any).__testTrigger("sub-1234", "update", { data: { result: { data: "data 1" }, more: true} }); |
| 132 | + (mockAbly as any).__testTrigger("sub-1234", "update", { data: { result: { data: "data 2" }, more: false} }); |
| 133 | + |
| 134 | + expect(log).toEqual([["forward", "operationName"]]) |
| 135 | + }) |
| 136 | +}) |
0 commit comments