Skip to content

fix: pass all data in .request() #426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions packages/messaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class Messaging {
context: this.messagingContext.context,
featureName: this.messagingContext.featureName,
method: name,
params: { data: '' },
params: data,
id
})
return this.transport.request(message)
Expand Down Expand Up @@ -127,6 +127,7 @@ export class MessagingTransport {
* @param {NotificationMessage} msg
* @returns {void}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
notify (msg) {
throw new Error("must implement 'notify'")
}
Expand All @@ -136,6 +137,7 @@ export class MessagingTransport {
* @param {{signal?: AbortSignal}} [options]
* @return {Promise<any>}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
request (msg, options = {}) {
throw new Error('must implement')
}
Expand All @@ -145,6 +147,7 @@ export class MessagingTransport {
* @param {(value: unknown) => void} callback
* @return {() => void}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
subscribe (msg, callback) {
throw new Error('must implement')
}
Expand Down Expand Up @@ -185,7 +188,7 @@ export class TestTransport {
return this.config.impl.notify(msg)
}

request (msg, options) {
request (msg) {
return this.config.impl.request(msg)
}

Expand Down
1 change: 1 addition & 0 deletions packages/messaging/lib/examples/payloads.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { NotificationMessage, RequestMessage, MessageResponse, SubscriptionEvent } from '../../index.js'

/**
Expand Down
8 changes: 4 additions & 4 deletions packages/messaging/lib/messaging.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface UnstableWebkit {

interface UnstableMockCall {
payload: import('../index.js').RequestMessage;
response?: Record<string, any>;
response?: Record<string, unknown>;
}

interface Window {
Expand All @@ -23,6 +23,6 @@ interface Window {
}
}

declare let windowsInteropPostMessage: any
declare let windowsInteropAddEventListener: any
declare let windowsInteropRemoveEventListener: any
declare let windowsInteropPostMessage: unknown
declare let windowsInteropAddEventListener: unknown
declare let windowsInteropRemoveEventListener: unknown
1 change: 1 addition & 0 deletions packages/messaging/lib/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* [[include:packages/messaging/lib/examples/windows.example.js]]```
*
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { MessagingTransport, NotificationMessage, RequestMessage } from '../index.js'

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/special-pages/pages/duckplayer/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ const MouseMove = {
/**
* Watch for inactivity and toggle toolbar accordingly
*/
handleFadeState: (e) => {
handleFadeState: () => {
if (MouseMove.timer) {
clearTimeout(MouseMove.timer)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/special-pages/tests/duckplayer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test.describe('duckplayer toolbar', () => {
await page.mouse.move(1, 1)
await duckplayer.infoTooltipIsHidden()
})
test('clicking on cog icon opens settings', async ({ page, context }, workerInfo) => {
test('clicking on cog icon opens settings', async ({ page }, workerInfo) => {
const duckplayer = DuckPlayerPage.create(page, workerInfo)
await duckplayer.openWithVideoID()
await duckplayer.hasLoadedIframe()
Expand Down
89 changes: 89 additions & 0 deletions unit-test/messaging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
Messaging,
MessagingContext,
TestTransportConfig,
RequestMessage, NotificationMessage, Subscription
} from '@duckduckgo/messaging'

describe('Messaging Transports', () => {
it('calls transport with a RequestMessage', () => {
const { messaging, transport } = createMessaging()

const spy = spyOn(transport, 'request')

messaging.request('helloWorld', { foo: 'bar' })

expect(spy).toHaveBeenCalledWith(new RequestMessage({
context: 'contentScopeScripts',
featureName: 'hello-world',
id: 'helloWorld.response',
method: 'helloWorld',
params: { foo: 'bar' }
}))
})
it('calls transport with a NotificationMessage', () => {
const { messaging, transport } = createMessaging()

const spy = spyOn(transport, 'notify')

messaging.notify('helloWorld', { foo: 'bar' })

expect(spy).toHaveBeenCalledWith(new NotificationMessage({
context: 'contentScopeScripts',
featureName: 'hello-world',
method: 'helloWorld',
params: { foo: 'bar' }
}))
})
it('calls transport with a Subscription', () => {
const { messaging, transport } = createMessaging()

const spy = spyOn(transport, 'subscribe')
const callback = jasmine.createSpy()

messaging.subscribe('helloWorld', callback)

expect(spy).toHaveBeenCalledWith(new Subscription({
context: 'contentScopeScripts',
featureName: 'hello-world',
subscriptionName: 'helloWorld'
}), callback)
})
})

/**
* Creates a test transport and Messaging instance for testing
*/
function createMessaging () {
/** @type {import("@duckduckgo/messaging").MessagingTransport} */
const transport = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
notify (msg) {
// test
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
request: (_msg) => {
// test
return Promise.resolve(null)
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
subscribe (_msg) {
// test
return () => {
// test teardown
}
}
}

const testTransportConfig = new TestTransportConfig(transport)

const messagingContext = new MessagingContext({
context: 'contentScopeScripts',
featureName: 'hello-world',
env: 'development'
})

const messaging = new Messaging(messagingContext, testTransportConfig)

return { transport, messaging }
}