Skip to content

Commit

Permalink
feat: test changes; load multiple tetsts (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomek-f authored May 9, 2023
1 parent 833d54c commit afa9344
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 5 deletions.
90 changes: 90 additions & 0 deletions test/array.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { afterAll, beforeAll, expect, test } from 'vitest';
import { preview } from 'vite';
import type { PreviewServer } from 'vite';
import { Browser, Page, chromium } from 'playwright';
import { TIMEOUT } from './constants';

let browser: Browser;
let server: PreviewServer;
let page: Page;

beforeAll(async () => {
browser = await chromium.launch({ headless: true });
server = await preview({ preview: { port: 3005 } });
page = await browser.newPage();
});

afterAll(async () => {
await browser.close();
await new Promise<void>((resolve, reject) => {
server.httpServer.close((error) => (error ? reject(error) : resolve()));
});
});

test(
'load array ok',
async () => {
try {
await page.goto('http://localhost:3005');
const scriptRefs = await page.evaluate(async () => {
const [a, b, c] = await window.simpleLoadScript([
'//code.jquery.com/jquery-2.2.3.js',
{
url: '//code.jquery.com/jquery-2.2.2.js',
attrs: { id: 'jquery2' },
},
{
url: '//code.jquery.com/jquery-2.2.1.js',
attrs: { id: 'jquery3' },
},
]);
return [a, b, c];
});
expect(scriptRefs.length).toBe(3);
const jquery1 = await page.$(
'head script[src="//code.jquery.com/jquery-2.2.3.js"]',
);
const jquery2 = await page.$('script#jquery2');
const jquery3 = await page.$('script#jquery3');
const src1 = await page.evaluate(
(script) => script?.getAttribute('src'),
jquery1,
);
const id2 = await page.evaluate((script) => script?.id, jquery2);
const id3 = await page.evaluate((script) => script?.id, jquery3);
expect(src1).toBe('//code.jquery.com/jquery-2.2.3.js');
expect(id2).toBe('jquery2');
expect(id3).toBe('jquery3');
} catch (err) {
expect(err).toBeUndefined();
}
},
TIMEOUT,
);

test(
'load array error',
async () => {
try {
await page.goto('http://localhost:3005');
await page.evaluate(async () => {
await window.simpleLoadScript([
'//wrong.domain/jquery-2.2.3.js',
{
url: '//code.jquery.com/jquery-2.2.2.js',
attrs: { id: 'jquery2' },
},
{
url: '//code.jquery.com/jquery-2.2.1.js',
attrs: { id: 'jquery3' },
},
]);
});
} catch (err) {
expect(
(err as Error).message.includes('Error: Loading script error'),
).toBe(true);
}
},
TIMEOUT,
);
2 changes: 1 addition & 1 deletion test/attrs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test(
);

test(
'dom not add attrs',
'do not add attrs',
async () => {
try {
await page.goto('http://localhost:3001');
Expand Down
8 changes: 4 additions & 4 deletions test/basic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ afterAll(async () => {
});

test(
'proper load',
'load url ok',
async () => {
try {
await page.goto('http://localhost:3000');
Expand All @@ -44,7 +44,7 @@ test(
);

test(
'proper load config',
'load config ok',
async () => {
try {
await page.goto('http://localhost:3000');
Expand All @@ -63,7 +63,7 @@ test(
);

test(
'error wrong url',
'wrong url error',
async () => {
try {
await page.goto('http://localhost:3000');
Expand All @@ -79,7 +79,7 @@ test(
TIMEOUT,
);

describe('error wrong config', () => {
describe('wrong config error', () => {
test(
'no param',
async () => {
Expand Down

0 comments on commit afa9344

Please sign in to comment.