forked from Sage-Bionetworks/synapse-web-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
908fc15
commit 7f9facf
Showing
5 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...ages/synapse-react-client/src/components/SharePageLinkButton/SharePageLinkButton.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/synapse-react-client/src/mocks/msw/handlers/shortIoHandlers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)), | ||
) | ||
}), | ||
] |