Skip to content

Commit

Permalink
add unit test for short io call
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-hodgson committed Sep 19, 2024
1 parent 908fc15 commit 7f9facf
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import React from 'react'
import { createWrapper } from '../../testutils/TestingLibraryUtils'
import SharePageLinkButton, {
SharePageLinkButtonProps,
} from './SharePageLinkButton'
import { MOCK_SHORT_IO_URL } from '../../mocks/mockShortIo'
import { server } from '../../mocks/msw/server'

function renderComponent(props: SharePageLinkButtonProps) {
return render(<SharePageLinkButton {...props} />, {
wrapper: createWrapper(),
})
}
describe('SharePageLinkButton', () => {
beforeAll(() => server.listen())
afterEach(() => server.restoreHandlers())
afterAll(() => server.close())
beforeEach(() => {
jest.clearAllMocks()
// Replace clipboard.writeText with a mock
Object.assign(navigator, {
clipboard: {
writeText: jest.fn().mockImplementation(() => Promise.resolve()),
},
})
})

it('Copies short.io response to clipboard', async () => {
renderComponent({ shortIoPublicApiKey: 'abc' })
expect(screen.queryByRole('alert')).not.toBeInTheDocument()

await userEvent.click(
screen.getByRole('button', { name: 'Share Page Link' }),
)
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(
MOCK_SHORT_IO_URL,
)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export const SharePageLinkButton: React.FunctionComponent<
domain: domain,
}),
})

if (!response.ok) {
const responseText = await response.text()
throw new Error(responseText)
Expand Down
12 changes: 12 additions & 0 deletions packages/synapse-react-client/src/mocks/mockShortIo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const MOCK_SHORT_IO_URL = 'https://short.io/abc123'
export const mockShortIoResponse: any = (
originalURL: string,
domain: string,
) => {
return {
id: '123456',
originalURL,
shortURL: MOCK_SHORT_IO_URL,
domain,
}
}
2 changes: 2 additions & 0 deletions packages/synapse-react-client/src/mocks/msw/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { getResetTwoFactorAuthHandlers } from './handlers/resetTwoFactorAuthHand
import { getMessageHandlers } from './handlers/messageHandlers'
import { getFeatureFlagsOverride } from './handlers/featureFlagHandlers'
import { getDoiHandler } from './handlers/doiHandlers'
import { getShortIoHandlers } from './handlers/shortIoHandlers'

// Simple utility type that just indicates that the response body could be an error like the Synapse backend may send.
export type SynapseApiResponse<T> = T | SynapseError
Expand Down Expand Up @@ -75,6 +76,7 @@ const getHandlers = (backendOrigin: string, portalOrigin?: string) => [
getFeatureFlagsOverride({ portalOrigin }),
...getHandlersForTableQuery(backendOrigin),
...getDoiHandler(backendOrigin),
...getShortIoHandlers(),
]

const handlers = getHandlers(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { rest } from 'msw'
import { mockShortIoResponse } from '../../../mocks/mockShortIo'

export const getShortIoHandlers = () => [
rest.post('https://api.short.io/links/public', async (req, res, ctx) => {
const body = await req.json()
return res(
ctx.status(200),
ctx.json(mockShortIoResponse(body.originalURL, body.domain)),
)
}),
]

0 comments on commit 7f9facf

Please sign in to comment.