Skip to content
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

add approval ui dimensions #88

Merged
merged 3 commits into from
Jun 10, 2022
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
48 changes: 47 additions & 1 deletion packages/embed/src/popup/popup.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createSubjectManager } from '../subjects'
import { mutableRef } from '../utils'
import { createPopupController } from './popup'
import { openPopup, redirectPopup } from './redirect-popup'
import { openPopup, redirectPopup, popupFeatures } from './redirect-popup'

jest.mock('./redirect-popup')

Expand All @@ -15,12 +15,16 @@ describe('registerSubscriptions', () => {
beforeEach(() => {
// reset mocks
;(openPopup as jest.Mock).mockReset()
;(popupFeatures as jest.Mock).mockReset()

subject = createSubjectManager()

// setup
popup.current = null
createPopupController(popup, subject)
;(popupFeatures as jest.Mock).mockImplementation(
(width, height) => `width=${width},height=${height}`
)
})

test('opens a popup when a transaction requires approval', () => {
Expand All @@ -41,6 +45,48 @@ describe('registerSubscriptions', () => {
expect(popup.current).toEqual(mockPopup)
})

test('opens a popup with custom dimensions', () => {
const mockPopup = jest.fn()

;(openPopup as jest.Mock).mockReturnValue(mockPopup)
subject.mode$.next({
popup: {
title: 'Test',
message: 'Test Message',
height: 34,
width: 52,
},
})
subject.approvalStarted$.next()

jest.runAllTimers()

expect((openPopup as jest.Mock).mock.calls[0][0]).toEqual(
'width=52,height=34'
)
expect(popup.current).toEqual(mockPopup)
})

test('opens a popup with default dimensions', () => {
const mockPopup = jest.fn()

;(openPopup as jest.Mock).mockReturnValue(mockPopup)
subject.mode$.next({
popup: {
title: 'Test',
message: 'Test Message',
},
})
subject.approvalStarted$.next()

jest.runAllTimers()

expect((openPopup as jest.Mock).mock.calls[0][0]).toEqual(
'width=500,height=589'
)
expect(popup.current).toEqual(mockPopup)
})

test('redirects popup when an approval url is available', () => {
const mockPopup = {
popup: jest.fn(),
Expand Down
11 changes: 9 additions & 2 deletions packages/embed/src/popup/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import { mutableRef } from '../utils'
import { redirectDocument } from './redirect-document'
import { openPopup, popupFeatures, redirectPopup } from './redirect-popup'

const DEFAULT_POPUP_WIDTH = 500
const DEFAULT_POPUP_HEIGHT = 589

export const createPopupController = (
popup = mutableRef<{ popup: Window; stopCallback: () => void }>(),
subject: SubjectManager
) => {
subject.approvalStarted$.subscribe(() => {
if (subject.mode$.value()?.popup) {
const mode = subject.mode$.value()
if (mode?.popup) {
popup.current = openPopup(
popupFeatures(500, 589),
popupFeatures(
mode.popup?.width || DEFAULT_POPUP_WIDTH,
mode.popup?.height || DEFAULT_POPUP_HEIGHT
),
redirectDocument(subject.mode$.value().popup),
() => subject.approvalCancelled$.next()
)
Expand Down
2 changes: 2 additions & 0 deletions packages/embed/src/subjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const createSubjectManager = () => {
popup?: {
message: string
title: string
width?: number
height?: number
}
overlay?: {
message
Expand Down
4 changes: 4 additions & 0 deletions packages/embed/webpack.dev.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const Dotenv = require('dotenv-webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { DefinePlugin } = require('webpack')

module.exports = {
mode: 'development',
Expand Down Expand Up @@ -49,5 +50,8 @@ module.exports = {
new HtmlWebpackPlugin({
title: 'Gr4vy - Embed',
}),
new DefinePlugin({
douglaseggleton marked this conversation as resolved.
Show resolved Hide resolved
PACKAGE_VERSION: JSON.stringify(process.env.npm_package_version),
}),
],
}