-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #753 from adobecom/MWPW-154096
MWPW-154096 Add unit tests for acom-widget
- Loading branch information
Showing
7 changed files
with
467 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,72 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
/* eslint-disable compat/compat */ | ||
/* eslint-disable no-undef */ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
|
||
const mockFetch = jest.fn(() => Promise.resolve({ | ||
json: () => Promise.resolve({ | ||
access_token: '123', | ||
discovery: { | ||
resources: { | ||
jobs: { status: { uri: 'https://pdfnow-dev.adobe.io/status' } }, | ||
assets: { | ||
upload: { uri: 'https://pdfnow-dev.adobe.io/upload' }, | ||
download_uri: { uri: 'https://pdfnow-dev.adobe.io/download' }, | ||
createpdf: { uri: 'https://pdfnow-dev.adobe.io/createpdf' }, | ||
}, | ||
}, | ||
}, | ||
}), | ||
ok: true, | ||
})); | ||
|
||
const xhrMock = { | ||
abort: jest.fn(), | ||
open: jest.fn(), | ||
setRequestHeader: jest.fn(), | ||
onreadystatechange: jest.fn(), | ||
progress: jest.fn(), | ||
upload: new EventTarget(), | ||
send: jest.fn(), | ||
readyState: 4, | ||
responseText: JSON.stringify({ | ||
uri: 'https://www.example.com', | ||
job_uri: 'https://www.example.com/job_uri', | ||
}), | ||
status: 201, | ||
}; | ||
|
||
describe('acom-widget block', () => { | ||
beforeEach(() => { | ||
document.head.innerHTML = fs.readFileSync(path.resolve(__dirname, './mocks/head.html'), 'utf8'); | ||
document.body.innerHTML = fs.readFileSync(path.resolve(__dirname, './mocks/body.html'), 'utf8'); | ||
window.fetch = mockFetch; | ||
window.XMLHttpRequest = jest.fn(() => xhrMock); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('upload PDF', async () => { | ||
const log = jest.spyOn(console, 'log'); | ||
|
||
delete window.location; | ||
window.location = new URL('https://localhost/acrobat/online/ai-chat-pdf.html?redirect=off'); | ||
|
||
const blockModule = await import('../../../acrobat/blocks/acom-widget/acom-widget.js'); | ||
|
||
const block = document.querySelector('.acom-widget'); | ||
await blockModule.default(block); | ||
|
||
const input = document.querySelector('input'); | ||
const file = new File(['hello'], 'hello.png', { type: 'image/png' }); | ||
await userEvent.upload(input, file); | ||
await xhrMock.onreadystatechange(); | ||
expect(log.mock.calls[0][0]).toContain('Blob Viewer 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
/* eslint-disable compat/compat */ | ||
/* eslint-disable no-undef */ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
import { delay } from '../../helpers/waitfor.js'; | ||
import init from '../../../acrobat/blocks/acom-widget/acom-widget.js'; | ||
|
||
const mockfetch = jest.fn(() => Promise.resolve({ | ||
json: () => Promise.resolve({ | ||
access_token: '123', | ||
discovery: { | ||
resources: { | ||
jobs: { status: { uri: 'https://pdfnow-dev.adobe.io/status' } }, | ||
assets: { | ||
upload: { uri: 'https://pdfnow-dev.adobe.io/upload' }, | ||
download_uri: { uri: 'https://pdfnow-dev.adobe.io/download' }, | ||
createpdf: { uri: 'https://pdfnow-dev.adobe.io/createpdf' }, | ||
}, | ||
}, | ||
}, | ||
}), | ||
ok: true, | ||
})); | ||
|
||
const mockXhr = { | ||
abort: jest.fn(), | ||
open: jest.fn(), | ||
setRequestHeader: jest.fn(), | ||
onreadystatechange: jest.fn(), | ||
progress: jest.fn(), | ||
upload: new EventTarget(), | ||
send: jest.fn(), | ||
readyState: 4, | ||
responseText: JSON.stringify({ | ||
uri: 'https://www.example.com/asseturi/', | ||
job_uri: 'https://www.example.com/job_uri', | ||
}), | ||
status: 201, | ||
}; | ||
|
||
describe('acom-widget block', () => { | ||
beforeEach(() => { | ||
document.head.innerHTML = fs.readFileSync(path.resolve(__dirname, './mocks/head.html'), 'utf8'); | ||
document.body.innerHTML = fs.readFileSync(path.resolve(__dirname, './mocks/body.html'), 'utf8'); | ||
window.fetch = mockfetch; | ||
window.XMLHttpRequest = jest.fn(() => mockXhr); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('upload PDF', async () => { | ||
window.alert = jest.fn(); | ||
|
||
delete window.localStorage.limit; | ||
|
||
delete window.location; | ||
window.location = new URL('https://localhost/acrobat/online/ai-chat-pdf.html'); | ||
|
||
const block = document.querySelector('.acom-widget'); | ||
await init(block); | ||
|
||
const input = document.querySelector('input'); | ||
const file = new File(['hello'], 'hello.png', { type: 'image/png' }); | ||
|
||
window.location = { assign: jest.fn() }; | ||
|
||
await userEvent.upload(input, file); | ||
|
||
await mockXhr.onreadystatechange(); | ||
|
||
await delay(100); | ||
|
||
expect(window.location.href).toMatch(/pdfNowAssetUri=https:\/\/www.example.com\/asseturi\//); | ||
}); | ||
}); |
Oops, something went wrong.