Skip to content

Commit

Permalink
Pass the api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
spring-raining committed Jan 12, 2025
1 parent a773dd2 commit 961f6be
Showing 1 changed file with 38 additions and 36 deletions.
74 changes: 38 additions & 36 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
import fileType from 'file-type';
import fs from 'node:fs';
import upath from 'upath';
import { expect, test } from 'vitest';
import { build } from '../src/index.js';
import { rootPath } from './command-util.js';
import { expect, it, vi } from 'vitest';
import { build, init, preview } from '../src/index.js';

const fixtureRoot = upath.resolve(rootPath, 'tests/fixtures/wood');
const fixtureFile = upath.join(fixtureRoot, 'index.html');
it('provides build function', async () => {
const mockedBuild = vi.hoisted(() => vi.fn());
vi.mock('../src/core/build', () => ({ build: mockedBuild }));

const localTmpDir = upath.join(rootPath, 'tmp');
fs.mkdirSync(localTmpDir, { recursive: true });

function cleanUp(filePath: string) {
try {
fs.unlinkSync(filePath);
} catch (err: any) {
if (err.code !== 'ENOENT') {
throw err;
}
}
}
await build({
config: 'vivliostyle.config.js',
});
expect(mockedBuild).toHaveBeenLastCalledWith(
expect.objectContaining({
config: 'vivliostyle.config.js',
}),
);
});

test('api generates pdf without errors', async () => {
const outputPath = upath.join(localTmpDir, 'test-api.pdf');
cleanUp(outputPath);
it('provides init function', async () => {
const mockedInit = vi.hoisted(() => vi.fn());
vi.mock('../src/core/init', () => ({ init: mockedInit }));

await build({
targets: [
{
path: outputPath,
format: 'pdf',
},
],
input: fixtureFile,
size: 'A4',
await init({
title: 'Vivliostyle',
});
expect(mockedInit).toHaveBeenLastCalledWith(
expect.objectContaining({
title: 'Vivliostyle',
}),
);
});

it('provides preview function', async () => {
const mockedPreview = vi.hoisted(() => vi.fn());
vi.mock('../src/core/preview', () => ({ preview: mockedPreview }));

// mimetype test
const type = await fileType.fromFile(outputPath);
expect(type!.mime).toEqual('application/pdf');
}, 120000);
await preview({
input: 'index.html',
});
expect(mockedPreview).toHaveBeenLastCalledWith(
expect.objectContaining({
input: { entry: 'index.html', format: 'webbook' },
}),
);
});

0 comments on commit 961f6be

Please sign in to comment.