Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate to nx workspace #1483

Draft
wants to merge 21 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: make tests runable again
  • Loading branch information
JosefBredereck committed Feb 26, 2023
commit 59f2bbec462524f7044888d2e3dca6aa9fc8e008
6 changes: 0 additions & 6 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ jobs:
- name: Setup the project
run: |
yarn run setup
npx lerna add @pattern-lab/engine-mustache --scope=@pattern-lab/core
npx lerna add @pattern-lab/engine-handlebars --scope=@pattern-lab/core
npx lerna add @pattern-lab/engine-underscore --scope=@pattern-lab/core
npx lerna add @pattern-lab/engine-liquid --scope=@pattern-lab/core
npx lerna add @pattern-lab/engine-twig --scope=@pattern-lab/core
npx lerna add @pattern-lab/engine-react --scope=@pattern-lab/core

- name: Run Unit Tests
run: yarn run test
6 changes: 5 additions & 1 deletion jest.preset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
const nxPreset = require('@nrwl/jest/preset').default;

module.exports = { ...nxPreset };
module.exports = {
...nxPreset,
collectCoverage: true,
coverageReporters: ['lcov', 'text'],
};
8 changes: 2 additions & 6 deletions packages/edition-node/tsconfig.spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"jest.config.ts",
"src/**/*.test.ts",
"src/**/*.spec.ts",
"src/**/*.d.ts"
]
"exclude": ["src/helpers/**/*.js"],
"include": ["src/helpers/test-helpers.jsrc/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
}
92 changes: 90 additions & 2 deletions packages/engine-handlebars/src/lib/engine-handlebars.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,95 @@
import { Pattern, PatternLabConfig } from '@pattern-lab/types';
import { EngineHandlebars } from './engine-handlebars';
import fs from 'fs-extra';
import path from 'path';

describe('EngineHandlebars', () => {
it('should work', () => {
expect(new EngineHandlebars()).toEqual('engine-handlebars');
let engine: EngineHandlebars;

beforeEach(() => {
engine = new EngineHandlebars();
});

describe('renderPattern()', () => {
it('should render the pattern with the given data', () => {
const pattern = {
template: '<h1>{{title}}</h1>',
patternPartial: 'test',
} as Pattern;
const data = { title: 'Hello, world!' };

const result = engine.renderPattern(pattern, data);
expect(result).resolves.toBe('<h1>Hello, world!</h1>');
});
});

describe('registerPartial()', () => {
it('should register the pattern as a partial', () => {
const pattern = {
template: '<h1>{{title}}</h1>',
patternPartial: 'test',
verbosePartial: 'test-verbose',
} as Pattern;

engine.registerPartial(pattern);

const patternWithPartial = {
template: '<div>{{> test}}</div>',
} as Pattern;
const data = { title: 'Hello, world!' };

const result = engine.renderPattern(patternWithPartial, data);
expect(result).resolves.toBe('<div><h1>Hello, world!</h1></div>');
});
});

describe('findPartials()', () => {
it('should find all partials in the pattern template', () => {
const pattern = {
template: '<h1>{{> header}}</h1><p>{{> body}}</p>',
patternPartial: 'test',
} as Pattern;
const result = engine.findPartials(pattern);
expect(result).toEqual(['{{> header}}', '{{> body}}']);
});
});

describe('findListItems()', () => {
it('should find all list items in the pattern template', () => {
const pattern = {
template: '{{#listItems.one}}{{label}}{{/listItems.one}}{{#listItems.two}}{{label}}{{/listItems.two}}',
patternPartial: 'test',
} as Pattern;
const result = engine.findListItems(pattern);
expect(result).toEqual(['{{#listItems.one}}', '{{#listItems.two}}']);
});
});

describe('findPartial()', () => {
it('should extract the partial name from a partial string', () => {
const result = engine.findPartial('{{> header}}');
expect(result).toBe('header');
});
});

describe('spawnMeta()', () => {
const normalizedExpect = (pathStr: string): string => expect.stringContaining(path.normalize(pathStr));

it('should spawn the meta pattern data', () => {
const readSpy = jest.spyOn(fs, 'readFileSync').mockReturnValue('File Content');
const outputSpy = jest.spyOn(fs, 'outputFileSync').mockImplementation(jest.fn);

const config = {
paths: { source: { meta: 'source/_meta' } },
} as PatternLabConfig;

engine.spawnMeta(config);

expect(readSpy).toHaveBeenCalledWith(normalizedExpect('engine-handlebars/_meta/_head.hbs'), 'utf8');
expect(readSpy).toHaveBeenCalledWith(normalizedExpect('engine-handlebars/_meta/_foot.hbs'), 'utf8');

expect(outputSpy).toHaveBeenCalledWith(normalizedExpect('source/_meta/_head.hbs'), 'File Content');
expect(outputSpy).toHaveBeenCalledWith(normalizedExpect('source/_meta/_foot.hbs'), 'File Content');
});
});
});
12 changes: 4 additions & 8 deletions packages/engine-handlebars/src/lib/engine-handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
*
*/

import { Pattern, PatternData, PatternLabConfig, PatternLabEngine } from '@pattern-lab/types';
import fs from 'fs-extra';
import path from 'path';
import Handlebars from 'handlebars';
import glob from 'glob';
import { PatternLabEngine, Pattern, PatternData, PatternLabConfig, PatternPartial } from '@pattern-lab/types';
import Handlebars from 'handlebars';
import path from 'path';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
declare function require(moduleName: string): any;
Expand All @@ -40,11 +40,7 @@ export class EngineHandlebars implements PatternLabEngine {
engineFileExtension = ['.hbs', '.handlebars'];
expandPartials = false;

renderPattern(pattern: Pattern, data: PatternData, partials?: PatternPartial): Promise<string> {
if (partials) {
this.engine.registerPartial(partials);
}

renderPattern(pattern: Pattern, data: PatternData): Promise<string> {
const compiled = this.engine.compile(this.escapeAtPartialBlock(pattern.template));

return Promise.resolve(compiled(data));
Expand Down
84 changes: 80 additions & 4 deletions packages/engine-liquid/src/lib/engine-liquid.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,83 @@
import { engineLiquid } from './engine-liquid';
import { Pattern, PatternLabConfig } from '@pattern-lab/types';
import fs from 'fs-extra';
import path from 'path';
import { EngineLiquid } from './engine-liquid';

describe('engineLiquid', () => {
it('should work', () => {
expect(engineLiquid()).toEqual('engine-liquid');
describe('EngineLiquid', () => {
let engine: EngineLiquid;

beforeEach(() => {
engine = new EngineLiquid();
});

describe('renderPattern()', () => {
it('should render the pattern with the given data', () => {
const pattern = {
template: '<h1>{{title}}</h1>',
patternPartial: 'test',
} as Pattern;
const data = { title: 'Hello, world!' };

const result = engine.renderPattern(pattern, data);
expect(result).resolves.toBe('<h1>Hello, world!</h1>');
});
});

describe('findPartials()', () => {
it('should find all partials in the pattern template', () => {
const pattern = {
template: `<section>{% layout 'default-layout.liquid' %}</section>
<h1>{% include 'header' %}</h1>
<p>{% include 'body', random_var: 'test' %}</p>
{% render 'footer.liquid' %}`,
patternPartial: 'test',
} as Pattern;
const result = engine.findPartials(pattern);
expect(result).toEqual([
"{% layout 'default-layout.liquid' %}",
"{% include 'header' %}",
"{% include 'body', random_var: 'test' %}",
"{% render 'footer.liquid' %}",
]);
});
});

describe('findListItems()', () => {
it('should find all list items in the pattern template', () => {
const pattern = {
template: '{{#listItems.one}}{{label}}{{/listItems.one}}{{#listItems.two}}{{label}}{{/listItems.two}}',
patternPartial: 'test',
} as Pattern;
const result = engine.findListItems(pattern);
expect(result).toEqual(['{{#listItems.one}}', '{{#listItems.two}}']);
});
});

describe('findPartial()', () => {
it('should extract the partial name from a partial string', () => {
const result = engine.findPartial('{{> header}}');
expect(result).toBe('header');
});
});

describe('spawnMeta()', () => {
const normalizedExpect = (pathStr: string): string => expect.stringContaining(path.normalize(pathStr));

it('should spawn the meta pattern data', () => {
const readSpy = jest.spyOn(fs, 'readFileSync').mockReturnValue('File Content');
const outputSpy = jest.spyOn(fs, 'outputFileSync').mockImplementation(jest.fn);

const config = {
paths: { source: { meta: 'source/_meta' } },
} as PatternLabConfig;

engine.spawnMeta(config);

expect(readSpy).toHaveBeenCalledWith(normalizedExpect('engine-liquid/_meta/_head.liquid'), 'utf8');
expect(readSpy).toHaveBeenCalledWith(normalizedExpect('engine-liquid/_meta/_foot.liquid'), 'utf8');

expect(outputSpy).toHaveBeenCalledWith(normalizedExpect('source/_meta/_head.liquid'), 'File Content');
expect(outputSpy).toHaveBeenCalledWith(normalizedExpect('source/_meta/_foot.liquid'), 'File Content');
});
});
});
13 changes: 5 additions & 8 deletions packages/engine-liquid/src/lib/engine-liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*
*/

import { PatternLabEngine, Pattern, PatternData, PatternLabConfig, PatternPartial } from '@pattern-lab/types';
import { Pattern, PatternData, PatternLabConfig, PatternLabEngine } from '@pattern-lab/types';
import fs from 'fs-extra';
import { Liquid } from 'liquidjs';
import path from 'path';
Expand All @@ -23,15 +23,11 @@ export class EngineLiquid implements PatternLabEngine {
engineFileExtension = ['.liquid', '.html'];
expandPartials = false;

renderPattern(pattern: Pattern, data: PatternData, _partials?: PatternPartial): Promise<string> {
renderPattern(pattern: Pattern, data: PatternData): Promise<string> {
return this.engine
.parseAndRender(pattern.template, data)
.then(function (html) {
return html;
})
.catch(function (ex) {
console.log(40, ex);
});
.then((html) => html)
.catch((ex) => console.log(40, ex));
}

registerPartial(_pattern: Pattern): void {
Expand All @@ -55,6 +51,7 @@ export class EngineLiquid implements PatternLabEngine {
}

findPartial(partialString: string): string {
// TODO: this is mustache specific code in the liquid engine - fix this
//strip out the template cruft
let foundPatternPartial = partialString
.replace('{{> ', '')
Expand Down
2 changes: 1 addition & 1 deletion packages/engine-liquid/src/lib/utilities/liquid.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// todo: document this exact regex long form.
export const partialsRE = new RegExp(
/{%\\include\\\s*?([\w\-./~]+)(?::[A-Za-z0-9-_|]+)?(?:(?:| )\(.*)?(?:\s*)?%}/g,
/{[%{]\s*.*?(?:render|include|layout)\(?\s*['"](.+?)['"][\s\S]*?\)?\s*[%}]}/g,
);

// look for an opening mustache include tag, followed by >=0 whitespaces
Expand Down
76 changes: 72 additions & 4 deletions packages/engine-mustache/src/lib/engine-mustache.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,75 @@
import { engineMustache } from './engine-mustache';
import { Pattern, PatternLabConfig } from '@pattern-lab/types';
import { EngineMustache } from './engine-mustache';
import fs from 'fs-extra';
import path from 'path';

describe('engineMustache', () => {
it('should work', () => {
expect(engineMustache()).toEqual('engine-mustache');
describe('EngineMustache', () => {
let engine: EngineMustache;

beforeEach(() => {
engine = new EngineMustache();
});

describe('renderPattern()', () => {
it('should render the pattern with the given data', () => {
const pattern = {
extendedTemplate: '<h1>{{title}}</h1>',
patternPartial: 'test',
} as Pattern;
const data = { title: 'Hello, world!' };

const result = engine.renderPattern(pattern, data);
expect(result).resolves.toBe('<h1>Hello, world!</h1>');
});
});

describe('findPartials()', () => {
it('should find all partials in the pattern template', () => {
const pattern = {
template: '<h1>{{> header}}</h1><p>{{> body}}</p>',
patternPartial: 'test',
} as Pattern;
const result = engine.findPartials(pattern);
expect(result).toEqual(['{{> header}}', '{{> body}}']);
});
});

describe('findListItems()', () => {
it('should find all list items in the pattern template', () => {
const pattern = {
template: '{{#listItems.one}}{{label}}{{/listItems.one}}{{#listItems.two}}{{label}}{{/listItems.two}}',
patternPartial: 'test',
} as Pattern;
const result = engine.findListItems(pattern);
expect(result).toEqual(['{{#listItems.one}}', '{{#listItems.two}}']);
});
});

describe('findPartial()', () => {
it('should extract the partial name from a partial string', () => {
const result = engine.findPartial('{{> header}}');
expect(result).toBe('header');
});
});

describe('spawnMeta()', () => {
const normalizedExpect = (pathStr: string): string => expect.stringContaining(path.normalize(pathStr));

it('should spawn the meta pattern data', () => {
const readSpy = jest.spyOn(fs, 'readFileSync').mockReturnValue('File Content');
const outputSpy = jest.spyOn(fs, 'outputFileSync').mockImplementation(jest.fn);

const config = {
paths: { source: { meta: 'source/_meta' } },
} as PatternLabConfig;

engine.spawnMeta(config);

expect(readSpy).toHaveBeenCalledWith(normalizedExpect('engine-mustache/_meta/_head.mustache'), 'utf8');
expect(readSpy).toHaveBeenCalledWith(normalizedExpect('engine-mustache/_meta/_foot.mustache'), 'utf8');

expect(outputSpy).toHaveBeenCalledWith(normalizedExpect('source/_meta/_head.mustache'), 'File Content');
expect(outputSpy).toHaveBeenCalledWith(normalizedExpect('source/_meta/_foot.mustache'), 'File Content');
});
});
});
6 changes: 3 additions & 3 deletions packages/engine-mustache/src/lib/engine-mustache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

import { PatternLabEngine, Pattern, PatternData, PatternLabConfig, PatternPartial } from '@pattern-lab/types';
import { PatternLabEngine, Pattern, PatternData, PatternLabConfig } from '@pattern-lab/types';
import fs from 'fs-extra';
import path from 'path';
import {
Expand All @@ -34,9 +34,9 @@ export class EngineMustache implements PatternLabEngine {
engineFileExtension = ['.mustache'];
expandPartials = true;

renderPattern(pattern: Pattern, data: PatternData, partials?: PatternPartial): Promise<string> {
renderPattern(pattern: Pattern, data: PatternData): Promise<string> {
try {
return Promise.resolve(this.engine.render(pattern.extendedTemplate, data, partials));
return Promise.resolve(this.engine.render(pattern.extendedTemplate, data));
} catch (e) {
console.log('e = ', e);
return Promise.reject(e);
Expand Down
Loading