-
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.
feat: Add support for CTA to link a document (#1082)
* add support for document cta and open pdfs in browser * small refactor and clean up tests * fix axios call to get blob * bug fixes and linter cleanup * use var to make it easier to read * remove unused test * update unit tests for announcement info * prevent undefined if error in cms validation * remove old comment --------- Co-authored-by: Jacob Capps <99674188+jcbcapps@users.noreply.github.com>
- Loading branch information
Showing
10 changed files
with
266 additions
and
69 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file removed
BIN
-916 KB
public/uploads/US Space Force Enlisted Rank Insig Info Sheet (1).pdf
Binary file not shown.
Binary file not shown.
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
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
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,40 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import axios from 'axios' | ||
import { isPdf, handleOpenPdfLink } from './openDocumentLink' | ||
|
||
jest.mock('axios', () => ({ | ||
get: jest.fn(() => Promise.resolve({ data: { blob: () => 'test' } })), | ||
})) | ||
|
||
// Mock URL.createObjectURL | ||
const mockCreateObjectURL = jest.fn() | ||
const mockRevokeObjectURL = jest.fn() | ||
URL.createObjectURL = mockCreateObjectURL | ||
URL.revokeObjectURL = mockRevokeObjectURL | ||
|
||
// Mock window.open | ||
const mockWindowOpen = jest.fn() | ||
window.open = mockWindowOpen | ||
|
||
describe('isPdf', () => { | ||
test('returns true if url ends with .pdf', () => { | ||
expect(isPdf('https://www.google.com/test.pdf')).toBe(true) | ||
}) | ||
|
||
test('returns false if url does not end with .pdf', () => { | ||
expect(isPdf('https://www.google.com/test')).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('handleOpenPdfLink', () => { | ||
test('opens a new window if the url is a pdf', async () => { | ||
const pdfString = 'https://www.google.com/test.pdf' | ||
await handleOpenPdfLink(pdfString) | ||
expect(axios.get).toHaveBeenCalled() | ||
expect(mockCreateObjectURL).toHaveBeenCalled() | ||
expect(mockWindowOpen).toHaveBeenCalled() | ||
expect(mockRevokeObjectURL).toHaveBeenCalled() | ||
}) | ||
}) |
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,19 @@ | ||
import axios from 'axios' | ||
|
||
export const isPdf = (url: string) => { | ||
return url.toString().endsWith('.pdf') | ||
} | ||
|
||
export const handleOpenPdfLink = async (pdfString: string) => { | ||
// Fetch the file from Keystone / S3 | ||
const res = await axios.get(pdfString, { responseType: 'blob' }) | ||
|
||
// Create a blob from the file and open it in a new tab | ||
const blobData = await res.data | ||
const file = new Blob([blobData], { type: 'application/pdf' }) | ||
const fileUrl = URL.createObjectURL(file) | ||
|
||
window.open(fileUrl) | ||
// Let the browser know not to keep the reference to the file any longer. | ||
URL.revokeObjectURL(fileUrl) | ||
} |
Oops, something went wrong.