Skip to content

Commit

Permalink
fix(test): add SettingsModule import to examples component tests, tes…
Browse files Browse the repository at this point in the history
…ts code style and improvements
  • Loading branch information
tomastrajan committed Sep 19, 2018
1 parent c4d4e3a commit 722fc81
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 37 deletions.
11 changes: 2 additions & 9 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { SharedModule } from '@app/shared';
import { TestingModule } from '@testing/utils';
import { CoreModule } from '@app/core';

import { AppComponent } from './app.component';
Expand All @@ -11,12 +9,7 @@ describe('AppComponent', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule,
RouterTestingModule,
SharedModule,
CoreModule
],
imports: [CoreModule, TestingModule],
declarations: [AppComponent]
}).compileComponents();
})
Expand Down
13 changes: 10 additions & 3 deletions src/app/core/local-storage/local-storage.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ describe('LocalStorageService', () => {
});
});

it('should load nested initial state with camel case properties and object value', () => {
service.setItem('TEST.PROP.SUB-PROP', 'value');
it('should load initial state with camel case property', () => {
service.setItem('TEST.SUB-PROP', 'value');
expect(LocalStorageService.loadInitialState()).toEqual({
test: { prop: { subProp: 'value' } }
test: { subProp: 'value' }
});
});

it('should load nested initial state with camel case properties', () => {
service.setItem('TEST.SUB-PROP.SUB-SUB-PROP', 'value');
expect(LocalStorageService.loadInitialState()).toEqual({
test: { subProp: { subSubProp: 'value' } }
});
});
});
3 changes: 2 additions & 1 deletion src/app/examples/examples/examples.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { CoreModule } from '@app/core';
import { TestingModule } from '@testing/utils';
import { SettingsModule } from '@app/settings';

import { ExamplesComponent } from './examples.component';

Expand All @@ -12,7 +13,7 @@ describe('ExamplesComponent', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
imports: [TestingModule, CoreModule],
imports: [TestingModule, CoreModule, SettingsModule],
declarations: [ExamplesComponent]
}).compileComponents();
})
Expand Down
27 changes: 20 additions & 7 deletions src/app/examples/form/components/form.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Store } from '@ngrx/store';

import { TestingModule } from '@testing/utils';
import { MockStore, TestingModule } from '@testing/utils';

import { State } from '../../examples.state';
import { FormState } from '../form.model';
import { initialState } from '../form.reducer';
import { FormComponent } from './form.component';

describe('FormComponent', () => {
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let store: MockStore<State>;

beforeEach(
async(() => {
TestBed.configureTestingModule({
imports: [TestingModule],
declarations: [FormComponent]
}).compileComponents();

store = TestBed.get(Store);
store.setState(createState(initialState));
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
})
);

beforeEach(() => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

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

function createState(formState: FormState) {
return {
examples: {
form: formState
}
} as State;
}
19 changes: 9 additions & 10 deletions src/app/examples/form/form.reducer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { FormState, Form } from './form.model';
import { Form } from './form.model';
import { formReducer, initialState } from './form.reducer';
import {
ActionFormUpdate,
ActionFormReset
} from '@app/examples/form/form.actions';
import { ActionFormUpdate, ActionFormReset } from './form.actions';

describe('FormReducer', () => {
const newForm: Form = {
const form: Form = {
username: 'test',
password: 'test',
email: 'test@test.test',
Expand All @@ -23,14 +20,16 @@ describe('FormReducer', () => {
});

it('should update the form', () => {
const action = new ActionFormUpdate({ form: newForm });
const action = new ActionFormUpdate({
form: { ...form, username: 'updated' }
});
const state = formReducer(initialState, action);
expect(state.form).toEqual(jasmine.objectContaining(newForm));
expect(state.form.username).toBe('updated');
});

it('should reset the form', () => {
const action = new ActionFormReset();
const state = formReducer({ form: newForm }, action);
expect(state).toBe(initialState);
const state = formReducer(undefined, action);
expect(state).toEqual(initialState);
});
});
5 changes: 2 additions & 3 deletions src/app/examples/stock-market/stock-market.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { TestBed, inject } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { CoreModule } from '@app/core';
import { HttpClientModule } from '@angular/common/http';

import { StockMarketService } from './stock-market.service';

describe('StockMarketService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule, CoreModule],
imports: [RouterTestingModule, HttpClientModule],
providers: [StockMarketService]
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe('TodosComponent', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
declarations: [TodosContainerComponent],
imports: [TestingModule]
imports: [TestingModule],
declarations: [TodosContainerComponent]
}).compileComponents();

store = TestBed.get(Store);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ describe('SettingsComponent', () => {
beforeEach(
async(() => {
TestBed.configureTestingModule({
declarations: [SettingsContainerComponent],
imports: [TestingModule]
imports: [TestingModule],
declarations: [SettingsContainerComponent]
}).compileComponents();

store = TestBed.get(Store);
Expand Down

0 comments on commit 722fc81

Please sign in to comment.