Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Add e2e test for guard
Browse files Browse the repository at this point in the history
  • Loading branch information
blackbaud-brandonstirnaman committed Jun 7, 2017
1 parent 92a777c commit e576df4
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 35 deletions.
43 changes: 41 additions & 2 deletions e2e/shared/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function prepareBuild(config) {
resetConfig();

// Create our server
httpServer = HttpServer.createServer({ root: tmp });
httpServer = HttpServer.createServer({ root: tmp, cache: 0 });

return new Promise((resolve, reject) => {
portfinder.getPortPromise()
Expand Down Expand Up @@ -193,6 +193,43 @@ function writeConfigServe(port) {
});
}

/**
* Write a file into the src/app folder -- Used for injecting files prior to build
* that we don't want to include in the skyux-template but need to test
*/
function writeAppFile(filePath, content) {
return new Promise((resolve, reject) => {
const resolvedFilePath = path.join(path.resolve(tmp), 'src', 'app', filePath);
fs.writeFile(resolvedFilePath, content, (err) => {
if (err) {
reject(err);
return;
}

resolve();
});
});
}

/**
* Remove file from the src/app folder -- Used for cleaning up after we've injected
* files for a specific test or group of tests
*/
function removeAppFile(filePath) {
return new Promise((resolve, reject) => {
const resolvedFilePath = path.join(path.resolve(tmp), 'src', 'app', filePath);

fs.unlink(resolvedFilePath, (err) => {
if (err) {
reject(err);
return;
}

resolve();
});
});
}

module.exports = {
afterAll: afterAll,
catchReject: catchReject,
Expand All @@ -203,5 +240,7 @@ module.exports = {
getExitCode: getExitCode,
prepareBuild: prepareBuild,
prepareServe: prepareServe,
tmp: tmp
tmp: tmp,
writeAppFile: writeAppFile,
removeAppFile: removeAppFile
};
7 changes: 7 additions & 0 deletions e2e/shared/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,12 @@ module.exports = {
nav.get(1).click();
expect(element(by.tagName('h1')).getText()).toBe('About our Team');
done();
},

respectGuardCanActivate: (done) => {
const nav = $$('.sky-navbar-item a');
nav.get(1).click();
expect(element(by.tagName('h1')).getText()).toBe('SKY UX Template');
done();
}
};
59 changes: 43 additions & 16 deletions e2e/skyux-build-aot.e2e-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,55 @@
const common = require('./shared/common');
const tests = require('./shared/tests');

describe('skyux build aot', () => {
function prepareBuild() {
const opts = { mode: 'easy', name: 'dist', compileMode: 'aot' };
return common.prepareBuild(opts)
.catch(console.error);
}

beforeAll((done) => {
const opts = { mode: 'easy', name: 'dist', compileMode: 'aot' };
common.prepareBuild(opts)
.then(done)
.catch(err => {
console.log(err);
done();
});
});
describe('skyux build aot', () => {
describe('w/base template', () => {
beforeAll((done) => prepareBuild().then(done));

afterAll(common.afterAll);
it('should have exitCode 0', tests.verifyExitCode);

it('should have exitCode 0', tests.verifyExitCode);
it('should generate expected static files', tests.verifyFiles);

it('should generate expected static files', tests.verifyFiles);
it('should render the home components', tests.renderHomeComponent);

it('should render the home components', tests.renderHomeComponent);
it('should render shared nav component', tests.renderSharedNavComponent);

it('should render shared nav component', tests.renderSharedNavComponent);
it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout);

it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout);
afterAll(common.afterAll);
});

describe('w/guard', () => {
beforeAll((done) => {
const guard = `
import { Injectable } from '@angular/core';
@Injectable()
export class AboutGuard {
canActivate(next: any, state: any) {
return false;
}
}
`;

common.writeAppFile('about/index.guard.ts', guard)
.then(() => prepareBuild())
.then(done)
.catch(console.error);
});

it('should not follow routerLink when guard returns false', tests.respectGuardCanActivate);

afterAll((done) => {
common.removeAppFile('about/index.guard.ts')
.then(() => common.afterAll())
.then(done)
.catch(console.error);
});
});
});
61 changes: 45 additions & 16 deletions e2e/skyux-build-jit.e2e-spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,59 @@
/*jshint jasmine: true, node: true */
'use strict';
const fs = require('fs');
const path = require('path');
const common = require('./shared/common');
const tests = require('./shared/tests');

describe('skyux build jit', () => {
function prepareBuild() {
const opts = { mode: 'easy', name: 'dist', compileMode: 'jit' };
return common.prepareBuild(opts)
.catch(err => console.error);
}

beforeAll((done) => {
const opts = { mode: 'easy', name: 'dist', compileMode: 'jit' };
common.prepareBuild(opts)
.then(done)
.catch(err => {
console.log(err);
done();
});
});
describe('skyux build jit', () => {
describe('w/base template', () => {
beforeAll((done) => prepareBuild().then(done));

afterAll(common.afterAll);
it('should have exitCode 0', tests.verifyExitCode);

it('should have exitCode 0', tests.verifyExitCode);
it('should generate expected static files', tests.verifyFiles);

it('should generate expected static files', tests.verifyFiles);
it('should render the home components', tests.renderHomeComponent);

it('should render the home components', tests.renderHomeComponent);
it('should render shared nav component', tests.renderSharedNavComponent);

it('should render shared nav component', tests.renderSharedNavComponent);
it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout);

it('should follow routerLink and render about component', tests.followRouterLinkRenderAbout);
afterAll(common.afterAll);
});

describe('w/guard', () => {
beforeAll((done) => {
const guard = `
import { Injectable } from '@angular/core';
@Injectable()
export class AboutGuard {
canActivate(next: any, state: any) {
return false;
}
}
`;

common.writeAppFile('about/index.guard.ts', guard)
.then(() => prepareBuild())
.then(done)
.catch(console.error);
});

it('should not follow routerLink when guard returns false', tests.respectGuardCanActivate);

afterAll((done) => {
common.removeAppFile('about/index.guard.ts')
.then(() => common.afterAll())
.then(done)
.catch(console.error);
});
});
});
2 changes: 1 addition & 1 deletion lib/sky-pages-route-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ function extractGuard(file) {
throw new Error(`As a best practice, only export one guard per file in ${file}`);
}

result = { path: file, name: match[1] };
result = { path: path.resolve(file), name: match[1] };
}

return result;
Expand Down

0 comments on commit e576df4

Please sign in to comment.