Skip to content

Commit

Permalink
Merge pull request #187 from satanTime/docs
Browse files Browse the repository at this point in the history
docs: a better starting example
  • Loading branch information
satanTime authored Sep 12, 2020
2 parents c487e82 + bbb394d commit 37c74e7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 31 deletions.
46 changes: 30 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,32 +91,46 @@ describe('main', () => {
// or setting NO_ERRORS_SCHEMA.

// With ng-mocks it can be defined in the next way.
beforeEach(() =>
// AppComponent will stay as it is
beforeEach(() => {
// AppComponent will stay as it is,
// everything in AppModule will be mocked.
MockBuilder(AppComponent, AppModule)
// Adding a special config how to mock AppHeaderComponent.
.mock(AppHeaderComponent, {
render: {
// #menu template will be rendered together
// with mocked AppHeaderComponent.
menu: true,
},
})
);
return (
MockBuilder(AppComponent, AppModule)
// Adding a special config how to mock AppHeaderComponent.
.mock(AppHeaderComponent, {
render: {
// #menu template will be rendered together
// with mocked AppHeaderComponent.
menu: true,
},
})
);
// the same as
// TestBed.configureTestingModule({
// imports: [
// MockModule(CommonModule),
// MockModule(RouterModule.forRoot([])),
// ],
// declarations: [
// AppComponent, // not mocked
// MockComponent(AppHeaderComponent),
// ],
// });
// return testBed.compileComponents();
});

it('example', () => {
const logoClickSpy = jasmine.createSpy();
// const logoClickSpy = jest.fn(); // in case of jest.
const logoClickSpy = typeof jest === 'undefined' ? jasmine.createSpy() : jest.fn();

// Instead of TestBed.createComponent(AppComponent)
// MockRender should be used.
// in beforeEach MockRender should be directly used
// in tests.
const fixture = MockRender(AppComponent, {
title: 'Fake Application',
logoClick: logoClickSpy,
});
// It creates a helper component
// with the template:
// with the next template:
// <app-root
// [title]="'Fake Application'"
// (logoClick)="logoClickSpy($event)"
Expand Down
45 changes: 30 additions & 15 deletions examples/MAIN/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// tslint:disable:object-literal-sort-keys member-ordering
// tslint:disable:object-literal-sort-keys member-ordering arrow-return-shorthand

// fix to support both jasmine and jest in the test
import { CommonModule } from '@angular/common';
Expand Down Expand Up @@ -73,31 +73,46 @@ describe('main', () => {
// or setting NO_ERRORS_SCHEMA.

// With ng-mocks it can be defined in the next way.
beforeEach(() =>
// AppComponent will stay as it is
beforeEach(() => {
// AppComponent will stay as it is,
// everything in AppModule will be mocked.
MockBuilder(AppComponent, AppModule)
// Adding a special config how to mock AppHeaderComponent.
.mock(AppHeaderComponent, {
render: {
// #menu template will be rendered together
// with mocked AppHeaderComponent.
menu: true,
},
})
);
return (
MockBuilder(AppComponent, AppModule)
// Adding a special config how to mock AppHeaderComponent.
.mock(AppHeaderComponent, {
render: {
// #menu template will be rendered together
// with mocked AppHeaderComponent.
menu: true,
},
})
);
// the same as
// TestBed.configureTestingModule({
// imports: [
// MockModule(CommonModule),
// MockModule(RouterModule.forRoot([])),
// ],
// declarations: [
// AppComponent, // not mocked
// MockComponent(AppHeaderComponent),
// ],
// });
// return testBed.compileComponents();
});

it('example', () => {
const logoClickSpy = typeof jest === 'undefined' ? jasmine.createSpy() : jest.fn();

// Instead of TestBed.createComponent(AppComponent)
// MockRender should be used.
// in beforeEach MockRender should be directly used
// in tests.
const fixture = MockRender(AppComponent, {
title: 'Fake Application',
logoClick: logoClickSpy,
});
// It creates a helper component
// with the template:
// with the next template:
// <app-root
// [title]="'Fake Application'"
// (logoClick)="logoClickSpy($event)"
Expand Down

0 comments on commit 37c74e7

Please sign in to comment.