Skip to content

Commit

Permalink
run tests on all EventTarget implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l committed Jul 8, 2022
1 parent ae66ecc commit 13c0a7a
Show file tree
Hide file tree
Showing 7 changed files with 473 additions and 176 deletions.
51 changes: 27 additions & 24 deletions packages/event-target/redis-event-target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
"url": "https://github.com/dotansimha/graphql-yoga.git",
"directory": "packages/event-target/redis-event-target"
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"scripts": {
"prepack": "bob prepack",
"check": "tsc --pretty --noEmit"
},
"keywords": [
Expand All @@ -21,27 +18,6 @@
],
"author": "Laurin Quast <[email protected]>",
"license": "MIT",
"buildOptions": {
"input": "./src/index.ts"
},
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
},
"./*": {
"require": "./dist/*.js",
"import": "./dist/*.mjs"
}
},
"typings": "dist/index.d.ts",
"typescript": {
"definition": "dist/index.d.ts"
},
"publishConfig": {
"directory": "dist",
"access": "public"
},
"dependencies": {
"@graphql-yoga/typed-event-target": "^0.0.0"
},
Expand All @@ -53,5 +29,32 @@
"bob-the-bundler": "^1.5.1",
"ioredis": "5.0.6",
"ioredis-mock": "8.2.2"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"require": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/cjs/index.js"
},
"import": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
},
"default": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
}
},
"./package.json": "./package.json"
},
"typings": "dist/typings/index.d.ts",
"typescript": {
"definition": "dist/typings/index.d.ts"
},
"publishConfig": {
"directory": "dist",
"access": "public"
}
}
41 changes: 24 additions & 17 deletions packages/event-target/typed-event-target/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"url": "https://github.com/dotansimha/graphql-yoga.git",
"directory": "packages/event-target/typed-event-target"
},
"main": "dist/index.js",
"module": "dist/index.mjs",
"scripts": {
"prepack": "bob prepack",
"check": "tsc --pretty --noEmit"
Expand All @@ -24,29 +22,38 @@
"buildOptions": {
"input": "./src/index.ts"
},
"dependencies": {
"@repeaterjs/repeater": "^3.0.4",
"tslib": "^2.3.1"
},
"devDependencies": {
"bob-the-bundler": "^1.5.1"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.mjs"
"require": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/cjs/index.js"
},
"import": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
},
"default": {
"types": "./dist/typings/index.d.ts",
"default": "./dist/esm/index.js"
}
},
"./*": {
"require": "./dist/*.js",
"import": "./dist/*.mjs"
}
"./package.json": "./package.json"
},
"typings": "dist/index.d.ts",
"typings": "dist/typings/index.d.ts",
"typescript": {
"definition": "dist/index.d.ts"
"definition": "dist/typings/index.d.ts"
},
"publishConfig": {
"directory": "dist",
"access": "public"
},
"dependencies": {
"@repeaterjs/repeater": "^3.0.4",
"tslib": "^2.3.1"
},
"devDependencies": {
"bob-the-bundler": "^1.5.1"
}
}
7 changes: 2 additions & 5 deletions packages/subscription/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"url": "https://github.com/dotansimha/graphql-yoga.git",
"directory": "packages/subscription"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"scripts": {
"check": "tsc --pretty --noEmit"
},
Expand All @@ -20,9 +18,8 @@
],
"author": "Laurin Quast <[email protected]>",
"license": "MIT",
"buildOptions": {
"input": "./src/index.ts"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"exports": {
".": {
"require": {
Expand Down
193 changes: 108 additions & 85 deletions packages/subscription/src/createPubSub.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Redis from 'ioredis-mock'
import { createRedisEventTarget } from '@graphql-yoga/redis-event-target'
import { createPubSub } from './createPubSub.js'

async function collectAsyncIterableValues<TType>(
Expand All @@ -18,97 +20,118 @@ describe('createPubSub', () => {
it('create', () => {
createPubSub()
})
it('subscribe to topic', async () => {
const pubSub = createPubSub<{
a: [number]
}>()

const sub = pubSub.subscribe('a')
const allValues = collectAsyncIterableValues(sub)
pubSub.publish('a', 1)
pubSub.publish('a', 2)
pubSub.publish('a', 3)

setImmediate(() => {
sub.return()
})

const result = await allValues
expect(result).toEqual([1, 2, 3])
})
it('subscribe to multiple topics', async () => {
const pubSub = createPubSub<{
a: [number]
b: [string]
}>()

const sub1 = pubSub.subscribe('a')
const sub2 = pubSub.subscribe('b')
const allValues1 = collectAsyncIterableValues(sub1)
const allValues2 = collectAsyncIterableValues(sub2)

pubSub.publish('a', 1)
pubSub.publish('b', '1')
pubSub.publish('a', 2)
pubSub.publish('b', '2')
pubSub.publish('a', 3)
pubSub.publish('b', '3')

setImmediate(() => {
sub1.return()
sub2.return()
describe.each([
['InMemory', () => new globalThis.EventTarget()],
[
'Redis',
() =>
createRedisEventTarget({
publishClient: new Redis({}),
subscribeClient: new Redis({}),
}),
],
])(`API %s`, (_, createEventTarget) => {
it('subscribe to topic', async () => {
const pubSub = createPubSub<{
a: [number]
}>({
eventTarget: createEventTarget(),
})

const sub = pubSub.subscribe('a')
const allValues = collectAsyncIterableValues(sub)
pubSub.publish('a', 1)
pubSub.publish('a', 2)
pubSub.publish('a', 3)

setImmediate(() => {
sub.return()
})

const result = await allValues
expect(result).toEqual([1, 2, 3])
})
it('subscribe to multiple topics', async () => {
const pubSub = createPubSub<{
a: [number]
b: [string]
}>({
eventTarget: createEventTarget(),
})

const result1 = await allValues1
const result2 = await allValues2
expect(result1).toEqual([1, 2, 3])
expect(result2).toEqual(['1', '2', '3'])
})
it('subscribe to fine-grained topic', async () => {
const pubSub = createPubSub<{
a: [id: string, payload: number]
}>()
const id1 = '1'
const sub1 = pubSub.subscribe('a', id1)
const allValues1 = collectAsyncIterableValues(sub1)
pubSub.publish('a', id1, 1)
pubSub.publish('a', id1, 2)
pubSub.publish('a', id1, 3)
setImmediate(() => {
sub1.return()
const sub1 = pubSub.subscribe('a')
const sub2 = pubSub.subscribe('b')
const allValues1 = collectAsyncIterableValues(sub1)
const allValues2 = collectAsyncIterableValues(sub2)

pubSub.publish('a', 1)
pubSub.publish('b', '1')
pubSub.publish('a', 2)
pubSub.publish('b', '2')
pubSub.publish('a', 3)
pubSub.publish('b', '3')

setImmediate(() => {
sub1.return()
sub2.return()
})

const result1 = await allValues1
const result2 = await allValues2
expect(result1).toEqual([1, 2, 3])
expect(result2).toEqual(['1', '2', '3'])
})
it('subscribe to fine-grained topic', async () => {
const pubSub = createPubSub<{
a: [id: string, payload: number]
}>({
eventTarget: createEventTarget(),
})
const id1 = '1'
const sub1 = pubSub.subscribe('a', id1)
const allValues1 = collectAsyncIterableValues(sub1)
pubSub.publish('a', id1, 1)
pubSub.publish('a', id1, 2)
pubSub.publish('a', id1, 3)
setImmediate(() => {
sub1.return()
})

const result1 = await allValues1
expect(result1).toEqual([1, 2, 3])
})
it('subscribe to multiple fine-grained topics', async () => {
const pubSub = createPubSub<{
a: [id: string, payload: number]
b: [id: string, payload: string]
}>()
const id1 = '1'
const id2 = '1'

const sub1 = pubSub.subscribe('a', id1)
const sub2 = pubSub.subscribe('b', id2)

const allValues1 = collectAsyncIterableValues(sub1)
const allValues2 = collectAsyncIterableValues(sub2)

pubSub.publish('a', id1, 1)
pubSub.publish('b', id1, '1')
pubSub.publish('a', id1, 2)
pubSub.publish('b', id1, '2')
pubSub.publish('a', id1, 3)
pubSub.publish('b', id1, '3')
setImmediate(() => {
sub1.return()
sub2.return()
const result1 = await allValues1
expect(result1).toEqual([1, 2, 3])
})
it('subscribe to multiple fine-grained topics', async () => {
const pubSub = createPubSub<{
a: [id: string, payload: number]
b: [id: string, payload: string]
}>({
eventTarget: createEventTarget(),
})
const id1 = '1'
const id2 = '1'

const sub1 = pubSub.subscribe('a', id1)
const sub2 = pubSub.subscribe('b', id2)

const result1 = await allValues1
const result2 = await allValues2
expect(result1).toEqual([1, 2, 3])
expect(result2).toEqual(['1', '2', '3'])
const allValues1 = collectAsyncIterableValues(sub1)
const allValues2 = collectAsyncIterableValues(sub2)

pubSub.publish('a', id1, 1)
pubSub.publish('b', id1, '1')
pubSub.publish('a', id1, 2)
pubSub.publish('b', id1, '2')
pubSub.publish('a', id1, 3)
pubSub.publish('b', id1, '3')
setImmediate(() => {
sub1.return()
sub2.return()
})

const result1 = await allValues1
const result2 = await allValues2
expect(result1).toEqual([1, 2, 3])
expect(result2).toEqual(['1', '2', '3'])
})
})
})
14 changes: 14 additions & 0 deletions patches/@types+ioredis-mock+5.6.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
diff --git a/node_modules/@types/ioredis-mock/index.d.ts b/node_modules/@types/ioredis-mock/index.d.ts
index 2da5af1..5b9815b 100755
--- a/node_modules/@types/ioredis-mock/index.d.ts
+++ b/node_modules/@types/ioredis-mock/index.d.ts
@@ -4,8 +4,7 @@
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

import RealIORedis = require('ioredis');
-
-export default RealIORedis;
+export default RealIORedis.default;
export type AddressFromResponse = RealIORedis.AddressFromResponse;
export type BooleanResponse = RealIORedis.BooleanResponse;
export type Callback<T> = RealIORedis.Callback<T>;
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
"@graphql-yoga/plugin-*": ["packages/plugins/*/src/index.ts"],
"@graphql-yoga/typed-event-target": [
"packages/event-target/typed-event-target/src/index.ts"
],
"@graphql-yoga/redis-event-target": [
"packages/event-target/redis-event-target/src/index.ts"
]
},
"jsx": "preserve"
Expand Down
Loading

0 comments on commit 13c0a7a

Please sign in to comment.