Skip to content

Commit

Permalink
test: zip
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohuoni committed Dec 29, 2023
1 parent 75ca3ec commit 0bcb2ce
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions modules/code-generator/tests/public/publisher/zip/zip.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import CodeGen from '../../../../src';
import fileSaver from 'file-saver';
import * as utils from '../../../../src/publisher/zip/utils';

jest.mock('file-saver');

describe('public/publisher/zip/zip', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should works', async () => {
const zip = CodeGen.publishers.zip({
outputPath: 'demo-output',
Expand All @@ -19,15 +27,15 @@ describe('public/publisher/zip/zip', () => {
],
};

expect(zip.getOutputPath()).toMatchInlineSnapshot(`"demo-output"`);
expect(zip.getOutputPath()).toMatchInlineSnapshot('"demo-output"');

expect(zip.getProject()).toMatchInlineSnapshot(`undefined`);
expect(zip.getProject()).toMatchInlineSnapshot('undefined');
zip.setProject(demoProject);
expect(zip.getProject()).toBeTruthy();

expect(zip.getOutputPath()).toMatchInlineSnapshot(`"demo-output"`);
expect(zip.getOutputPath()).toMatchInlineSnapshot('"demo-output"');
expect(zip.setOutputPath('output')).toBe(undefined);
expect(zip.getOutputPath()).toMatchInlineSnapshot(`"output"`);
expect(zip.getOutputPath()).toMatchInlineSnapshot('"output"');

const publishRes = await zip.publish({
project: demoProject,
Expand All @@ -41,4 +49,39 @@ describe('public/publisher/zip/zip', () => {
const zip = CodeGen.publishers.zip({});
expect(zip.publish()).rejects.toBeTruthy();
});

it('should publish the project as a zip file in the browser', async () => {
const zipContent = 'zip content';
const zipName = 'example-project';
jest.spyOn(utils, 'isNodeProcess').mockReturnValue(false);
// new Zip 里面也有平台判断,所以这里 mock
jest.spyOn(utils, 'generateProjectZip').mockResolvedValue(zipContent as any);
const spy = jest.spyOn(fileSaver, 'saveAs');

const zip = CodeGen.publishers.zip({
projectSlug: zipName,
});

const demoProject = {
name: 'demo',
dirs: [],
files: [
{
name: 'package',
ext: 'json',
content: '{ "name": "demo", "version": "1.0.0" }',
},
],
};

zip.setProject(demoProject);
const publishRes = await zip.publish({
project: demoProject,
});

expect(publishRes.success).toBeTruthy();
expect(spy).toBeCalledWith(zipContent, `${zipName}.zip`);
spy.mockReset();
spy.mockRestore();
});
});

0 comments on commit 0bcb2ce

Please sign in to comment.