Skip to content

Commit

Permalink
chore: add Angular example (#8905)
Browse files Browse the repository at this point in the history
  • Loading branch information
artemrudenko authored and SimenB committed Sep 6, 2019
1 parent 9279a3a commit 8054b94
Show file tree
Hide file tree
Showing 12 changed files with 303 additions and 2 deletions.
24 changes: 24 additions & 0 deletions examples/angular/.babelrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

module.exports = {
plugins: [
'babel-plugin-transform-typescript-metadata',
['@babel/plugin-proposal-decorators', {legacy: true}],
'@babel/plugin-proposal-class-properties',
],
presets: [
[
'@babel/preset-env',
{
shippedProposals: true,
targets: {node: 8},
},
],
'@babel/preset-typescript',
]
};
42 changes: 42 additions & 0 deletions examples/angular/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {ComponentFixture, TestBed, async} from '@angular/core/testing';

import {AppComponent} from './app.component';
import {DataService} from './shared/data.service';

const title = 'Test';
const getTitleFn = jest.fn().mockReturnValue(title);
const dataServiceSpy = jest.fn().mockImplementation(
(): Partial<DataService> => ({
getTitle: getTitleFn,
})
);

describe('AppComponent', () => {
let fixture: ComponentFixture<AppComponent>;
let app: AppComponent;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [AppComponent],
providers: [{provide: DataService, useClass: dataServiceSpy}],
}).compileComponents();
fixture = TestBed.createComponent(AppComponent);
app = fixture.debugElement.componentInstance;
}));

it('should create the app', () => {
expect(app).toBeTruthy();
});

it(`should have as title 'angular'`, () => {
expect(app.title).toEqual(title);
});

it('should render title in a h1 tag', () => {
fixture.detectChanges();
const compiled = fixture.debugElement.nativeElement;
expect(compiled.querySelector('h1').textContent).toContain(
`Welcome to ${title}!`
);
});
});
15 changes: 15 additions & 0 deletions examples/angular/app.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Component} from '@angular/core';

import {DataService} from './shared/data.service';

@Component({
selector: 'app-root',
template: '<h1>Welcome to {{ title }}!</h1>',
})
export class AppComponent {
public title: string;

constructor(dataService: DataService) {
this.title = dataService.getTitle();
}
}
11 changes: 11 additions & 0 deletions examples/angular/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';

import {AppComponent} from './app.component';

@NgModule({
bootstrap: [AppComponent],
declarations: [AppComponent],
imports: [BrowserModule],
})
export class AppModule {}
10 changes: 10 additions & 0 deletions examples/angular/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
moduleFileExtensions: ['ts', 'html', 'js', 'json'],
setupFilesAfterEnv: ['<rootDir>/setupJest.js'],
transform: {
'^.+\\.[t|j]s$': [
'babel-jest',
{configFile: require.resolve('./.babelrc')},
],
},
};
31 changes: 31 additions & 0 deletions examples/angular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "angular",
"version": "0.0.1",
"scripts": {
"test": "jest"
},
"private": true,
"dependencies": {
"@angular/common": "^8.2.5",
"@angular/compiler": "^8.2.5",
"@angular/core": "^8.2.5",
"@angular/forms": "^8.2.5",
"@angular/platform-browser": "^8.2.5",
"@angular/platform-browser-dynamic": "^8.2.5",
"core-js": "^3.2.1",
"rxjs": "^6.5.3",
"typescript": "*",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@babel/plugin-proposal-class-properties": "*",
"@babel/plugin-proposal-decorators": "*",
"@babel/preset-env": "*",
"@babel/preset-typescript": "*",
"babel-jest": "*",
"babel-plugin-transform-typescript-metadata": "*",
"@types/jest": "*",
"jest": "*",
"jest-zone-patch": "*"
}
}
18 changes: 18 additions & 0 deletions examples/angular/setupJest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

require('core-js/proposals/reflect-metadata');
require('zone.js/dist/zone.js');
require('zone.js/dist/proxy.js');
require('zone.js/dist/sync-test');
require('zone.js/dist/async-test');
require('zone.js/dist/fake-async-test');
// eslint-disable-next-line import/no-extraneous-dependencies
require('jest-zone-patch');

const {getTestBed} = require('@angular/core/testing');
const testingModule = require('@angular/platform-browser-dynamic/testing');

getTestBed().initTestEnvironment(
testingModule.BrowserDynamicTestingModule,
testingModule.platformBrowserDynamicTesting()
);
30 changes: 30 additions & 0 deletions examples/angular/shared/data.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {TestBed} from '@angular/core/testing';

import {DataService} from './data.service';
import {SubService} from './sub.service';

const title = 'SubTest';
const getTitleFn = jest.fn().mockReturnValue(title);
const subServiceSpy = jest.fn().mockImplementation(() => ({
getTitle: getTitleFn,
}));

describe('Service: DataService', () => {
let service: DataService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [DataService, {provide: SubService, useClass: subServiceSpy}],
});

service = TestBed.get(DataService);
});

it('should create service', () => {
expect(service).toBeTruthy();
});

it('should return the right title', () => {
expect(service.getTitle()).toEqual(title);
});
});
12 changes: 12 additions & 0 deletions examples/angular/shared/data.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Injectable} from '@angular/core';

import {SubService} from './sub.service';

@Injectable()
export class DataService {
constructor(private subService: SubService) {}

getTitle() {
return this.subService.getTitle();
}
}
18 changes: 18 additions & 0 deletions examples/angular/shared/sub.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {TestBed} from '@angular/core/testing';

import {SubService} from './sub.service';

describe('Service: SubService', () => {
let service: SubService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [SubService],
});
service = TestBed.get(SubService);
});

it('should create service', () => {
expect(service).toBeTruthy();
});
});
8 changes: 8 additions & 0 deletions examples/angular/shared/sub.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {Injectable} from '@angular/core';

@Injectable()
export class SubService {
public getTitle() {
return 'Angular App with Jest24';
}
}
86 changes: 84 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,48 @@
# yarn lockfile v1


"@angular/common@^8.2.5":
version "8.2.5"
resolved "https://registry.yarnpkg.com/@angular/common/-/common-8.2.5.tgz#4155df1ca4a6ac50a82b9cbf40dbc357f214e040"
integrity sha512-7iSDLVhS+jbVRkECpbTzU9+6IQPS3Wl0CF73EA0sdzPbTC2GKvGfM9WLnIZZIxewkii6Wn1Yb0x0qRdWMT2STA==
dependencies:
tslib "^1.9.0"

"@angular/compiler@^8.2.5":
version "8.2.5"
resolved "https://registry.yarnpkg.com/@angular/compiler/-/compiler-8.2.5.tgz#015928a7698677484f5ff38054c5b12ea71a8e44"
integrity sha512-u3OgSBTupn9DN1uDF+NmXqN7w9m6bbrIalJkwdw+kFXnXt8JpdIeJmeV2jN4wLuGp6A3sWS1ze+6u4kpFHMqTw==
dependencies:
tslib "^1.9.0"

"@angular/core@^8.2.5":
version "8.2.5"
resolved "https://registry.yarnpkg.com/@angular/core/-/core-8.2.5.tgz#c94f41c81abc318c9597285648f790b88ca7bfa6"
integrity sha512-cBEiHhLE8VFIdB53seR+nQYNQFlNloKgD7ro26eMazvRF94wBSzO9VrD3+/XmNWdIYibU7PBaXhDCOKTe+ZSHw==
dependencies:
tslib "^1.9.0"

"@angular/forms@^8.2.5":
version "8.2.5"
resolved "https://registry.yarnpkg.com/@angular/forms/-/forms-8.2.5.tgz#3d86574b344ffbe51403bf7237585cf42daaa8ab"
integrity sha512-USJdzopslLC7JVMu7v58SA/g0NWeQeAM16qcR4LHj+wdMbJ+5G64LdZQe9vEHRdgGpgrZU4c2ODAwDEa1MzIDA==
dependencies:
tslib "^1.9.0"

"@angular/platform-browser-dynamic@^8.2.5":
version "8.2.5"
resolved "https://registry.yarnpkg.com/@angular/platform-browser-dynamic/-/platform-browser-dynamic-8.2.5.tgz#9d10e6a6ab11e541892049f41ea6244723a2c9ab"
integrity sha512-4Ewg8I3T0t6/ClLt5ZFZ6ncDTqvEyI84h0K1cnNTsyoup3QKrY/FnklFbZbNl4ONVioHS6fkEg3R+xt1WthhYQ==
dependencies:
tslib "^1.9.0"

"@angular/platform-browser@^8.2.5":
version "8.2.5"
resolved "https://registry.yarnpkg.com/@angular/platform-browser/-/platform-browser-8.2.5.tgz#2c71197a7ae1bf3b09f17f0c81c027d3142f8bf2"
integrity sha512-JIm4uOcgQq0oX1oTzRbQpwxFYAEYKiLi/uAPUf2CZeU2lVxMkhScAW0b8+tVFLIJ7IaVx5d2QxZ6HK81r+QSVg==
dependencies:
tslib "^1.9.0"

"@babel/code-frame@7.5.5", "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.5.5":
version "7.5.5"
resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.5.5.tgz#bc0782f6d69f7b7d49531219699b988f669a8f9d"
Expand Down Expand Up @@ -72,7 +114,7 @@
"@babel/traverse" "^7.4.4"
"@babel/types" "^7.4.4"

"@babel/helper-create-class-features-plugin@^7.5.5":
"@babel/helper-create-class-features-plugin@^7.4.4", "@babel/helper-create-class-features-plugin@^7.5.5":
version "7.5.5"
resolved "https://registry.yarnpkg.com/@babel/helper-create-class-features-plugin/-/helper-create-class-features-plugin-7.5.5.tgz#401f302c8ddbc0edd36f7c6b2887d8fa1122e5a4"
integrity sha512-ZsxkyYiRA7Bg+ZTRpPvB6AbOFKTFFK4LrvTet8lInm0V468MWCaSYJE+I7v2z2r8KNLtYiV+K5kTCnR7dvyZjg==
Expand Down Expand Up @@ -262,6 +304,15 @@
"@babel/helper-create-class-features-plugin" "^7.5.5"
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-proposal-decorators@*":
version "7.4.4"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-decorators/-/plugin-proposal-decorators-7.4.4.tgz#de9b2a1a8ab0196f378e2a82f10b6e2a36f21cc0"
integrity sha512-z7MpQz3XC/iQJWXH9y+MaWcLPNSMY9RQSthrLzak8R8hCj0fuyNk+Dzi9kfNe/JxxlWQ2g7wkABbgWjW36MTcw==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.4.4"
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-decorators" "^7.2.0"

"@babel/plugin-proposal-dynamic-import@^7.5.0":
version "7.5.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-proposal-dynamic-import/-/plugin-proposal-dynamic-import-7.5.0.tgz#e532202db4838723691b10a67b8ce509e397c506"
Expand Down Expand Up @@ -341,6 +392,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-decorators@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-decorators/-/plugin-syntax-decorators-7.2.0.tgz#c50b1b957dcc69e4b1127b65e1c33eef61570c1b"
integrity sha512-38QdqVoXdHUQfTpZo3rQwqQdWtCn5tMv4uV6r2RMfTqNBuv4ZBhz79SfaQWKTVmxHjeFv/DnXVC/+agHCklYWA==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-dynamic-import@^7.0.0", "@babel/plugin-syntax-dynamic-import@^7.2.0":
version "7.2.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-dynamic-import/-/plugin-syntax-dynamic-import-7.2.0.tgz#69c159ffaf4998122161ad8ebc5e6d1f55df8612"
Expand Down Expand Up @@ -2971,6 +3029,13 @@ babel-plugin-syntax-trailing-function-commas@^7.0.0-beta.0:
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-trailing-function-commas/-/babel-plugin-syntax-trailing-function-commas-7.0.0-beta.0.tgz#aa213c1435e2bffeb6fca842287ef534ad05d5cf"
integrity sha512-Xj9XuRuz3nTSbaTXWv3itLOcxyF4oPD8douBBmj7U9BBC6nEBYfyOJYQMf/8PJAFotC62UY5dFfIGEPr7WswzQ==

babel-plugin-transform-typescript-metadata@*:
version "0.2.2"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-typescript-metadata/-/babel-plugin-transform-typescript-metadata-0.2.2.tgz#fc44611187409ed9d5cb372ca2f85939a359cada"
integrity sha512-wWRw1p3IDNZN8l3UOLgeScq9Nhh4lvxT7ZOvUzYi5sMw8lUWW+KdRlnBeQKehHAsgDs5TR6PvMJmsQ6e6NmKCg==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

babel-plugin-typescript-strip-namespaces@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/babel-plugin-typescript-strip-namespaces/-/babel-plugin-typescript-strip-namespaces-1.1.1.tgz#160433b17e424b57cf72e3b4d8f08195ad28d7fd"
Expand Down Expand Up @@ -4296,7 +4361,7 @@ core-js@^2.2.2, core-js@^2.4.0, core-js@^2.4.1, core-js@^2.6.5:
resolved "https://registry.yarnpkg.com/core-js/-/core-js-2.6.9.tgz#6b4b214620c834152e179323727fc19741b084f2"
integrity sha512-HOpZf6eXmnl7la+cUdMnLvUxKNqLUzJvgIziQ0DiF3JwSImNphIqdGqzj6hIKyX04MmV0poclQ7+wjWvxQyR2A==

core-js@^3.0.0, core-js@^3.1.3:
core-js@^3.0.0, core-js@^3.1.3, core-js@^3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.2.1.tgz#cd41f38534da6cc59f7db050fe67307de9868b09"
integrity sha512-Qa5XSVefSVPRxy2XfUC13WbvqkxhkwB3ve+pgCQveNgYzbM/UxZeu1dcOX/xr4UmfUd+muuvsaxilQzCyUurMw==
Expand Down Expand Up @@ -8118,6 +8183,11 @@ jest-worker@24.0.0-alpha.6:
dependencies:
merge-stream "^1.0.1"

jest-zone-patch@*:
version "0.0.10"
resolved "https://registry.yarnpkg.com/jest-zone-patch/-/jest-zone-patch-0.0.10.tgz#58252f44ab4aad45aaed62a705819577b9709b82"
integrity sha512-K5uHLHgMgi2Eyj74gbY+xSeGGekb5U48bXsgDwgipRbFdaekyZK+TAcp8auamqU4UjrAt5S4sIUZz/2bBNyTTA==

jpegtran-bin@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jpegtran-bin/-/jpegtran-bin-4.0.0.tgz#d00aed809fba7aa6f30817e59eee4ddf198f8f10"
Expand Down Expand Up @@ -12134,6 +12204,13 @@ rxjs@^6.4.0:
dependencies:
tslib "^1.9.0"

rxjs@^6.5.3:
version "6.5.3"
resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-6.5.3.tgz#510e26317f4db91a7eb1de77d9dd9ba0a4899a3a"
integrity sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==
dependencies:
tslib "^1.9.0"

safe-buffer@5.1.2, safe-buffer@~5.1.0, safe-buffer@~5.1.1:
version "5.1.2"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
Expand Down Expand Up @@ -14454,3 +14531,8 @@ yeast@0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/yeast/-/yeast-0.1.2.tgz#008e06d8094320c372dbc2f8ed76a0ca6c8ac419"
integrity sha1-AI4G2AlDIMNy28L47XagymyKxBk=

zone.js@~0.9.1:
version "0.9.1"
resolved "https://registry.yarnpkg.com/zone.js/-/zone.js-0.9.1.tgz#e37c6e5c54c13fae4de26b5ffe8d8e9212da6d9b"
integrity sha512-GkPiJL8jifSrKReKaTZ5jkhrMEgXbXYC+IPo1iquBjayRa0q86w3Dipjn8b415jpitMExe9lV8iTsv8tk3DGag==

0 comments on commit 8054b94

Please sign in to comment.