Skip to content

Commit

Permalink
Merge pull request #753 from adobecom/MWPW-154096
Browse files Browse the repository at this point in the history
MWPW-154096 Add unit tests for acom-widget
  • Loading branch information
Blainegunn authored Aug 15, 2024
2 parents 86e1f96 + dc9af77 commit 4babb81
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 4 deletions.
148 changes: 144 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
"scripts": {
"test": "npm run wtr && npm run jest",
"wtr": "wtr \"./test/**/*.test.(js|html)\" --node-resolve --port=2000 --coverage --concurrent-browsers 4",
"wtr:file": "wtr --node-resolve --port=2000 --coverage --concurrent-browsers 4",
"wtr:watch": "npm run wtr -- --watch",
"wtr:file:watch": "npm run wtr:file -- --watch",
"int": "wtr \"./test/integration/**/*.int.(js|html)\" --node-resolve --port=2000 --concurrent-browsers 3 --config wtr-integration.config.mjs",
"int:watch": "npm run int -- --watch",
"int3": "wtr \"./test/integration/**/*.int.(js|html)\" --node-resolve --port=2000 --concurrent-browsers 3 --config wtr-int-browsers.config.mjs",
"int3:watch": "npm run int3 -- --watch",
"jest": "jest --testPathPattern=test --coverage --coverageDirectory=coverage/jest",
"jest:file": "jest --coverage --coverageDirectory=coverage/jest",
"jest:watch": "npm run jest -- --watchAll",
"jest:file:watch": "npm run jest:file -- --watchAll",
"lcov": "lcov -a coverage/jest/lcov.info -a coverage/wtr/lcov.info -o coverage/lcov.info",
"lint": "npm run lint:js && npm run lint:css",
"lint:js": "eslint .",
Expand Down Expand Up @@ -63,6 +67,7 @@
"dependencies": {
"@75lb/deep-merge": "^1.1.2",
"@rollup/plugin-replace": "5.0.5",
"@testing-library/user-event": "^14.5.2",
"@web/dev-server-rollup": "0.6.1",
"@web/test-runner": "0.18.1",
"@web/test-runner-commands": "0.9.0",
Expand Down
72 changes: 72 additions & 0 deletions test/blocks/acom-widget/acom-widget-redirect.jest.js
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:');
});
});
81 changes: 81 additions & 0 deletions test/blocks/acom-widget/acom-widget.jest.js
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\//);
});
});
Loading

0 comments on commit 4babb81

Please sign in to comment.