Skip to content

Commit

Permalink
update the test cases to use a single test file
Browse files Browse the repository at this point in the history
  • Loading branch information
Anmol1696 committed Nov 11, 2024
1 parent 00d8ca9 commit 375ac56
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 114 deletions.

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

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

1 change: 1 addition & 0 deletions __output__/schema-data/state-export.bundle.js

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

1 change: 0 additions & 1 deletion __output__/schema-data/state-export/bundle.js

This file was deleted.

121 changes: 121 additions & 0 deletions packages/build/__tests__/__snapshots__/schemaExtractor.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`schemaExtractorPlugin should extract a basic contract with public and private methods 1`] = `
{
"methods": [],
"state": {
"properties": {
"count": {
"type": "number",
},
"startCoin": {
"properties": {
"amount": {
"type": "string",
},
"denom": {
"type": "string",
},
},
"type": "object",
},
"tokens": {
"items": {
"properties": {
"amount": {
"type": "string",
},
"denom": {
"type": "string",
},
},
"type": "object",
},
"type": "array",
},
},
"type": "object",
},
}
`;

exports[`schemaExtractorPlugin should extract methods from classes public methods 1`] = `
{
"methods": [
{
"functionName": "increment",
"parameters": [],
"returnType": {
"type": "any",
},
},
{
"functionName": "addToken",
"parameters": [
{
"name": "denom",
"type": {
"type": "string",
},
},
{
"name": "amount",
"type": {
"type": "string",
},
},
],
"returnType": {
"type": "any",
},
},
{
"functionName": "removeToken",
"parameters": [
{
"name": "index",
"type": {
"type": "number",
},
},
],
"returnType": {
"type": "any",
},
},
],
"state": {
"properties": {
"count": {
"type": "number",
},
"startCoin": {
"properties": {
"amount": {
"type": "string",
},
"denom": {
"type": "string",
},
},
"type": "object",
},
"tokens": {
"items": {
"properties": {
"amount": {
"type": "string",
},
"denom": {
"type": "string",
},
},
"type": "object",
},
"type": "array",
},
},
"type": "object",
},
}
`;
58 changes: 58 additions & 0 deletions packages/build/__tests__/schemaExtractor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs from 'fs/promises';
import { join, resolve } from 'path';
import { HyperwebBuild, HyperwebBuildOptions, schemaExtractorPlugin } from '../src';

const outputDir = resolve(join(__dirname, '/../../../__output__/schema-data'));

const runTest = async (fixtureName: string) => {
const fixtureDir = resolve(join(__dirname, `/../../../__fixtures__/schema-data/${fixtureName}`));
const schemaOutputPath = join(outputDir, `${fixtureName}.schema.json`);

const buildOptions: Partial<HyperwebBuildOptions> = {
entryPoints: [join(fixtureDir, 'index.ts')],
outfile: join(outputDir, `${fixtureName}.bundle.js`),
customPlugins: [
schemaExtractorPlugin({
outputPath: schemaOutputPath,
baseDir: fixtureDir,
include: [/\.ts$/],
exclude: [/node_modules/, /\.test\.ts$/],
}),
],
};

await HyperwebBuild.build(buildOptions);
const schemaContent = await fs.readFile(schemaOutputPath, 'utf-8');
return JSON.parse(schemaContent);
};

describe('schemaExtractorPlugin', () => {
it('should extract a basic contract with public and private methods', async () => {
const schemaData = await runTest('state-export');

expect(schemaData).toHaveProperty('state');
expect(schemaData.state).toHaveProperty('type', 'object');
expect(schemaData.state).toHaveProperty('properties');

expect(schemaData).toHaveProperty('methods');
expect(schemaData.methods).toEqual([]);

expect(schemaData).toMatchSnapshot();
});

it('should extract methods from classes public methods', async () => {
const schemaData = await runTest('public-methods');

expect(schemaData).toHaveProperty('state');
expect(schemaData.state).toHaveProperty('type', 'object');
expect(schemaData.state).toHaveProperty('properties');

const methodNames = schemaData.methods.map((method: any) => method.functionName);
expect(methodNames).toContain('addToken');
expect(methodNames).toContain('increment');
expect(methodNames).toContain('removeToken');
expect(methodNames).not.toContain('reset');

expect(schemaData).toMatchSnapshot();
});
});
57 changes: 0 additions & 57 deletions packages/build/__tests__/schemaMethods.test.ts

This file was deleted.

54 changes: 0 additions & 54 deletions packages/build/__tests__/schemaState.test.ts

This file was deleted.

0 comments on commit 375ac56

Please sign in to comment.