Skip to content

Commit

Permalink
Role definition changed (#4307)
Browse files Browse the repository at this point in the history
  • Loading branch information
helen-dikareva authored and AndreyBelym committed Oct 2, 2019
1 parent d6758b6 commit bfd8985
Show file tree
Hide file tree
Showing 31 changed files with 5,224 additions and 2,085 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/node_modules
/ts-defs
.idea
.vscode
/lib
Expand Down
55 changes: 53 additions & 2 deletions Gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ const del = require('del');
const fs = require('fs');
const path = require('path');
const { Transform } = require('stream');
const { promisify } = require('util');
const globby = require('globby');
const open = require('open');
const open = require('open');
const connect = require('connect');
const spawn = require('cross-spawn');
const serveStatic = require('serve-static');
Expand All @@ -35,6 +36,7 @@ const checkLicenses = require('./test/dependency-licenses-checker');
const packageInfo = require('./package');
const getPublishTags = require('./docker/get-publish-tags');

const readFile = promisify(fs.readFile);

gulpStep.install();

Expand Down Expand Up @@ -151,6 +153,12 @@ process.env.PATH = NODE_MODULE_BINS + path.delimiter + process.env.PATH + path.d

let websiteServer = null;

function promisifyStream (stream) {
return new Promise((resolve, reject) => {
stream.on('end', resolve).on('error', reject);
});
}

gulp.task('audit', () => {
return npmAuditor()
.then(result => {
Expand Down Expand Up @@ -193,6 +201,49 @@ gulp.task('check-licenses', () => {
});

// Build
const EMPTY_COMMENT_REGEXP = /^\s*\/\/\s*$/mg;
const EMPTY_LINES_REGEXP = /^\s*$/mg;
const NEWLINE_REGEXP = /^/mg;
const IDNENT_SPACE_REGEXP = /^\s*\n(\s*)\S/;
const SPACE = ' ';
const INDENT_SPACE_COUNT = 4;

gulp.step('ts-defs', async () => {
const partialPaths = await globby('ts-defs-src/*/**/*.d.ts');
const partials = {};

for (const partialPath of partialPaths)
partials[path.basename(partialPath)] = String(await readFile(partialPath));

const stream = gulp
.src('ts-defs-src/*.mustache')
.pipe(mustache(
{
allowReferences: false,

format: () => (text, render) => {
const renderedText = render(text);

const indent = IDNENT_SPACE_REGEXP.exec(text);
const indentLength = indent[1].length - INDENT_SPACE_COUNT;

return renderedText
.replace(NEWLINE_REGEXP, SPACE.repeat(indentLength))
.replace(EMPTY_COMMENT_REGEXP, '')
.replace(EMPTY_LINES_REGEXP, '');
}
},
{},
partials
))
.pipe(rename(file => {
file.extname = '';
}))
.pipe(gulp.dest('./ts-defs/'));


await promisifyStream(stream);
});

gulp.step('client-scripts-bundle', () => {
return gulp
Expand Down Expand Up @@ -325,7 +376,7 @@ gulp.step('images', () => {
.pipe(gulp.dest('lib'));
});

gulp.step('package-content', gulp.parallel('server-scripts', 'client-scripts', 'styles', 'images', 'templates'));
gulp.step('package-content', gulp.parallel('ts-defs', 'server-scripts', 'client-scripts', 'styles', 'images', 'templates'));

gulp.task('fast-build', gulp.series('clean', 'package-content'));

Expand Down
41 changes: 39 additions & 2 deletions test/server/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const fs = require('fs');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const globby = require('globby');
const { TEST_RUN_ERRORS } = require('../../lib/errors/types');
const exportableLib = require('../../lib/api/exportable-lib');
const createStackFilter = require('../../lib/errors/create-stack-filter.js');
Expand Down Expand Up @@ -243,10 +244,10 @@ describe('Compiler', function () {
it('Should complile ts-definitions successfully with the `--strict` option enabled', function () {
this.timeout(60000);

const tscPath = path.resolve('node_modules/typescript/bin/tsc');
const tscPath = path.resolve('node_modules/.bin/tsc');
const defsPath = path.resolve('ts-defs/index.d.ts');
const args = '--strict';
const command = `node ${tscPath} ${defsPath} ${args} --target ES6`;
const command = `${tscPath} ${defsPath} ${args} --target ES6 --noEmit`;

return new Promise(resolve => {
exec(command, (error, stdout) => {
Expand Down Expand Up @@ -318,6 +319,42 @@ describe('Compiler', function () {

expect(createProgram.callCount).eql(1);
});

it('Should provide correct globals in TestCafe scripts', async () => {
this.timeout(60000);

const tscPath = path.resolve('node_modules/.bin/tsc');
const defsPath = path.resolve('ts-defs/testcafe-scripts.d.ts');
const scriptPaths = await globby('test/server/data/test-suites/typescript-testcafe-scripts-defs/*.ts');
const command = `${tscPath} ${defsPath} ${scriptPaths.join(' ')} --target ES6 --noEmit`;

return new Promise(resolve => {
exec(command, (error, stdout) => {
resolve({ error, stdout });
});
}).then(value => {
expect(value.stdout).eql('');
expect(value.error).is.null;
});
});

it('Should provide correct globals for selector and client-functions only', async () => {
this.timeout(60000);

const tscPath = path.resolve('node_modules/.bin/tsc');
const defsPath = path.resolve('ts-defs/selectors.d.ts');
const scriptPaths = await globby('test/server/data/test-suites/typescript-selectors-defs/*.ts');
const command = `${tscPath} ${defsPath} ${scriptPaths.join(' ')} --target ES6 --noEmit`;

return new Promise(resolve => {
exec(command, (error, stdout) => {
resolve({ error, stdout });
});
}).then(value => {
expect(value.stdout).eql('');
expect(value.error).is.null;
});
});
});


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { expect } from 'chai';


(async () => {
const dep = true;
const someFunc = ClientFunction(() => dep, { dependencies: { dep } });

const getLocation = ClientFunction(() => document.location.toString());
const getUserAgent = ClientFunction(() => navigator.userAgent);

await someFunc();

await getUserAgent();

const getElementText = ClientFunction((className: string, idx: number) => {
return document.querySelectorAll('.' + className)[idx].textContent;
});

const answer1 = await getElementText('answer', 1);

if (!answer1)
return;

expect(answer1.trim()).eql('42');

const location1 = await getLocation();

expect(location1).eql('http://localhost:3000/fixtures/api/es-next/client-function/pages/index.html');


ClientFunction(async() => Promise.resolve());

ClientFunction(function*() {
yield 1;
});

const res1 = await ClientFunction(() => {
return Promise
.resolve()
.then(()=> {
return new Promise(resolve => {
window.setTimeout(() => resolve(42), 100);
});
});
})();

expect(res1).eql(42);

const res2 = await ClientFunction(() => {
const obj = {1: '1', '2': 2};

return typeof obj === 'object' ? JSON.stringify(Object.keys(obj)) : null;
})();

if (!res2)
return;

expect(JSON.parse(res2)).eql(['1', '2']);

const fn1 = ClientFunction(() => {
throw new Error('Hey ya!');
});

await fn1();

const fn2 = ClientFunction(() => {
return Promise.resolve().then(()=> {
throw new Error('42');
});
});

await fn2();

const selectByClassName: any = ClientFunction((className: string) => document.querySelectorAll('.' + className));
const nthByClass = ClientFunction((className: string, n: number) => selectByClassName(className)[n], {dependencies: {selectByClassName}});

nthByClass('foo', 42);

const fn3 = ClientFunction((re: any, err: any, undef: any, nan: any) => {
return re instanceof RegExp &&
re.source === '\\S+' &&
err instanceof Error &&
err.message === 'Hey!' &&
undef === void 0 &&
isNaN(nan);
});

const res3 = await fn3(/\S+/ig, new Error('Hey!'), void 0, NaN);

expect(res3).to.be.true;

const fn4 = ClientFunction((): [RegExp, Error, any, number] => {
return [/\S+/ig, new Error('Hey!'), void 0, NaN];
});

const res4 = await fn4();

expect(res4[0]).to.be.instanceof(RegExp);
expect(res4[0].source).eql('\\S+');
expect(res4[1]).to.be.instanceof(Error);
expect(res4[1].message).eql('Hey!');
expect(res4[2]).to.be.undefined;
expect(res4[3]).to.be.NaN;

function getAnswer() {
return new Promise(resolve => {
setTimeout(() => resolve(42), 30);
});
}

const hfn1 = ClientFunction((fn: Function) => fn());
const answer = await hfn1(getAnswer);

expect(answer).eql(42);

const hfn2 = ClientFunction((fn: Function) => fn());

await hfn2(async() => Promise.resolve());

const hfn3 = ClientFunction((fn: Function) => fn());
const location2 = await hfn3(getLocation);

expect(location2).eql('http://localhost:3000/fixtures/api/es-next/client-function/pages/index.html');

getLocation();

const getSomeNodes = ClientFunction(() => {
const answer = document.querySelector('.answer');

if (!answer)
return [];

return [answer.childNodes[0], document];
});

await getSomeNodes();
})();
Loading

0 comments on commit bfd8985

Please sign in to comment.