Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration Angular Material Test Harness #517

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,32 @@ import { FormComponent } from './form.component';
import { selectFormState } from '../form.selectors';
import { Form } from '../form.model';

import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatInputHarness } from '@angular/material/input/testing';
import { MatButtonHarness } from '@angular/material/button/testing';

describe('FormComponent', () => {
let store: MockStore;
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let dispatchSpy: jasmine.Spy;
let loader: HarnessLoader;

const getInput = (fieldName: string) =>
fixture.debugElement.query(By.css(`[formControlName="${fieldName}"]`));
loader.getHarness(
MatInputHarness.with({ selector: `[formControlName="${fieldName}"]` })
);

const getSaveButton = () =>
fixture.debugElement.queryAll(By.css('.buttons button'))[1];
loader.getHarness(
MatButtonHarness.with({ text: 'anms.examples.form.save' })
);

const getResetButton = () =>
fixture.debugElement.queryAll(By.css('.buttons button'))[2];
const getResetButton = async () =>
loader.getHarness(
MatButtonHarness.with({ text: 'anms.examples.form.reset' })
);

beforeEach(async () => {
TestBed.configureTestingModule({
Expand All @@ -38,23 +50,17 @@ describe('FormComponent', () => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);

dispatchSpy = spyOn(store, 'dispatch');
});

it('should save form', async () => {
const inputEvent = new KeyboardEvent('input', {
bubbles: true,
cancelable: true,
shiftKey: false
});
const usernameInput = await getInput('username');
const saveButton = await getSaveButton();

getInput('username').nativeElement.value = 'tomastrajan';
getInput('username').nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();

getSaveButton().nativeElement.click();
fixture.detectChanges();
await usernameInput.setValue('tomastrajan');
await saveButton.click();

expect(dispatchSpy).toHaveBeenCalledTimes(1);
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe('[Form] Update');
Expand All @@ -71,21 +77,15 @@ describe('FormComponent', () => {
});

it('should reset form', async () => {
const inputEvent = new KeyboardEvent('input', {
bubbles: true,
cancelable: true,
shiftKey: false
});

getInput('username').nativeElement.value = 'tomastrajan';
getInput('username').nativeElement.dispatchEvent(inputEvent);
fixture.detectChanges();
const usernameInput = await getInput('username');
const resetButton = await getResetButton();

getResetButton().nativeElement.click();
fixture.detectChanges();
await usernameInput.setValue('tomastrajan');
await resetButton.click();
const usernameValue = await usernameInput.getValue();

expect(dispatchSpy).toHaveBeenCalledTimes(1);
expect(dispatchSpy.calls.mostRecent().args[0].type).toBe('[Form] Reset');
expect(getInput('username').nativeElement.value).toBe('');
expect(usernameValue).toBe('');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ import {
selectTodos,
selectTodosState
} from '../todos.selectors';
import { HarnessLoader, TestKey } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
import { MatButtonHarness } from '@angular/material/button/testing';
import {
MatMenuItemHarness,
MatMenuHarness
} from '@angular/material/menu/testing';
import { MatInputHarness } from '@angular/material/input/testing';

describe('TodosComponent', () => {
let store: MockStore;
Expand All @@ -23,14 +31,16 @@ describe('TodosComponent', () => {
let dispatchSpy: jasmine.Spy;
let mockSelectTodos: MemoizedSelector<any, Todo[]>;
let mockSelectRemoveDoneTodosDisabled: MemoizedSelector<any, boolean>;
let loader: HarnessLoader;

const getOpenFilterButton = () =>
fixture.debugElement.query(By.css('.todos-filter'));
loader.getHarness(MatButtonHarness.with({ selector: '.todos-filter' }));

const getFilterActiveButton = () =>
fixture.debugElement.queryAll(
By.css('.todos-filter-menu-overlay button')
)[2];
const getFilterActiveButton = async () => {
const menu = await loader.getHarness(MatMenuHarness);
const items = await menu.getItems();
return items[2];
};

const getTodoInput = () =>
fixture.debugElement.query(By.css('anms-big-input input'));
Expand All @@ -39,15 +49,19 @@ describe('TodosComponent', () => {

const getTodoItem = () => fixture.debugElement.query(By.css('.todo-label'));

const getAddTodoButton = () =>
fixture.debugElement
.queryAll(By.css('anms-big-input-action'))[0]
.query(By.css('button'));
const getAddTodoButton = async () => {
const buttons = await loader.getAllHarnesses(
MatButtonHarness.with({ selector: 'anms-big-input-action button' })
);
return buttons[0];
};

const getRemoveDoneTodosButton = () =>
fixture.debugElement
.queryAll(By.css('anms-big-input-action'))[1]
.query(By.css('button'));
const getRemoveDoneTodosButton = async () => {
const buttons = await loader.getAllHarnesses(
MatButtonHarness.with({ selector: 'anms-big-input-action button' })
);
return buttons[1];
};

beforeEach(async () => {
TestBed.configureTestingModule({
Expand All @@ -65,6 +79,8 @@ describe('TodosComponent', () => {
fixture = TestBed.createComponent(TodosContainerComponent);
component = fixture.componentInstance;
fixture.detectChanges();
loader = TestbedHarnessEnvironment.loader(fixture);

dispatchSpy = spyOn(store, 'dispatch');
});

Expand All @@ -84,7 +100,7 @@ describe('TodosComponent', () => {
expect(getTodoItems()[0].nativeElement.textContent.trim()).toBe('test');
});

it('should dispatch remove "DONE" todos action', () => {
it('should dispatch remove "DONE" todos action', async () => {
mockSelectTodos.setResult([
{ id: '1', name: 'test 1', done: true },
{ id: '2', name: 'test 2', done: false }
Expand All @@ -94,15 +110,16 @@ describe('TodosComponent', () => {
fixture.detectChanges();
dispatchSpy.calls.reset();

getRemoveDoneTodosButton().nativeElement.click();
const removeDoneTodosButton = await getRemoveDoneTodosButton();
await removeDoneTodosButton.click();

expect(dispatchSpy).toHaveBeenCalledTimes(1);
expect(dispatchSpy).toHaveBeenCalledWith(
todoActions.actionTodosRemoveDone()
);
});

it('should dispatch add todo action', () => {
it('should dispatch add todo action', async () => {
fixture.detectChanges();
dispatchSpy.calls.reset();

Expand All @@ -116,25 +133,23 @@ describe('TodosComponent', () => {
getTodoInput().nativeElement.dispatchEvent(keyUpEvent);
fixture.detectChanges();

getAddTodoButton().nativeElement.click();

fixture.detectChanges();
const addTodoButton = await getAddTodoButton();
await addTodoButton.click();

expect(getTodoInput().nativeElement.value).toBe('');
expect(dispatchSpy).toHaveBeenCalledTimes(1);
console.log(dispatchSpy.calls.mostRecent().args[0]);
expect(dispatchSpy.calls.mostRecent().args[0].name).toBe('hello world');
});

it('should dispatch filter todo action', () => {
fixture.detectChanges();
it('should dispatch filter todo action', async () => {
dispatchSpy.calls.reset();

getOpenFilterButton().nativeElement.click();
fixture.detectChanges();
const openFilterButton = await getOpenFilterButton();
await openFilterButton.click();

getFilterActiveButton().nativeElement.click();
fixture.detectChanges();
const filterActiveButton = await getFilterActiveButton();
await filterActiveButton.click();

expect(dispatchSpy).toHaveBeenCalledTimes(1);
expect(dispatchSpy).toHaveBeenCalledWith(
Expand All @@ -157,13 +172,16 @@ describe('TodosComponent', () => {
);
});

it('should disable remove done todos button if no todo is done', () => {
it('should disable remove done todos button if no todo is done', async () => {
fixture.detectChanges();

expect(getRemoveDoneTodosButton().nativeElement.disabled).toBe(true);
const removeDoneTodosButton = await getRemoveDoneTodosButton();
const removeDoneTodosButtonIsDisabled = await removeDoneTodosButton.isDisabled();

expect(removeDoneTodosButtonIsDisabled).toBe(true);
});

it('should disable add new todo button if input length is less than 4', () => {
it('should disable add new todo button if input length is less than 4', async () => {
fixture.detectChanges();

const keyUpEvent = new KeyboardEvent('keyup', {
Expand All @@ -175,14 +193,17 @@ describe('TodosComponent', () => {
getTodoInput().nativeElement.value = 'add';
getTodoInput().nativeElement.dispatchEvent(keyUpEvent);
fixture.detectChanges();
const addTodoButton = await getAddTodoButton();
let addTodoButtonIsDisabled = await addTodoButton.isDisabled();

expect(getAddTodoButton().nativeElement.disabled).toBe(true);
expect(addTodoButtonIsDisabled).toBe(true);

getTodoInput().nativeElement.value = 'long enough';
getTodoInput().nativeElement.dispatchEvent(keyUpEvent);
fixture.detectChanges();

expect(getAddTodoButton().nativeElement.disabled).toBe(false);
addTodoButtonIsDisabled = await addTodoButton.isDisabled();
expect(addTodoButtonIsDisabled).toBe(false);
});

it('should clear new todo input value on ESC key press', () => {
Expand Down