-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ts): refactor name to age component
Change test to plain integration test -> use the original implementation of agify.service
- Loading branch information
Showing
6 changed files
with
51 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { fakeAsync, tick } from '@angular/core/testing'; | ||
import { of } from 'rxjs'; | ||
|
||
describe('Async operations', () => { | ||
it('should show usage of fakeAsync', fakeAsync(() => { | ||
let a = 1; | ||
of(1).subscribe(() => { | ||
a += 1; | ||
expect(a).toBe(2); | ||
}); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 31 additions & 23 deletions
54
apps/testing-showcase/src/app/name-to-age/name-to-age.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,52 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { ReactiveFormsModule } from '@angular/forms'; | ||
import { render, screen } from '@testing-library/angular'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { createSpyFromClass } from 'jest-auto-spies'; | ||
import { of } from 'rxjs'; | ||
import { AgifyService } from '../../../../../libs/name-to-age/src/index'; | ||
import { AgifyService, defaultResponse } from '../../../../../libs/name-to-age/src/index'; | ||
import { ButtonModule } from '../../../../../libs/ui/src/index'; | ||
|
||
import { NameToAgeComponent } from './name-to-age.component'; | ||
|
||
describe('NameToAgeComponent', () => { | ||
const agifyService = createSpyFromClass(AgifyService); | ||
let agifyService: AgifyService; | ||
|
||
const setup = async () => | ||
render(NameToAgeComponent, { | ||
imports: [ | ||
ReactiveFormsModule, | ||
CommonModule, | ||
ButtonModule | ||
], | ||
providers: [ | ||
{ provide: AgifyService, useValue: agifyService } | ||
ButtonModule, | ||
HttpClientTestingModule | ||
], | ||
}); | ||
|
||
it('should create', async () => { | ||
const renderResult = await setup(); | ||
expect(renderResult.fixture.componentInstance).toBeInstanceOf(NameToAgeComponent); | ||
describe('Integration Test', () => { | ||
it('should create', async () => { | ||
const renderResult = await setup(); | ||
expect(renderResult.fixture.componentInstance).toBeInstanceOf(NameToAgeComponent); | ||
|
||
}); | ||
}); | ||
|
||
it('should show age response after entering a name', async () => { | ||
const renderResult = await setup(); | ||
agifyService = TestBed.inject(AgifyService); | ||
const agifyServiceGetSpy = jest.spyOn(agifyService, 'getAge'); | ||
const component = renderResult.fixture.componentInstance; | ||
|
||
it('should show age response after entering a name', async () => { | ||
const renderResult = await setup(); | ||
const component = renderResult.fixture.componentInstance; | ||
agifyService.getAge.mockReturnValue(of(33)); | ||
await userEvent.type(screen.getByTestId('name'), 'dennis'); | ||
await userEvent.click(screen.getByTestId('ts-btn')); | ||
expect(component.searchForm.get('name')?.value).toEqual('dennis'); | ||
expect(agifyService.getAge).toHaveBeenCalledWith('dennis'); | ||
expect(await screen.findByText('33')).toBeTruthy(); | ||
|
||
screen.logTestingPlaygroundURL(); | ||
await userEvent.type(screen.getByTestId('name'), 'dennis'); | ||
await userEvent.click(screen.getByTestId('ts-btn')); | ||
|
||
TestBed.inject(HttpTestingController) | ||
.expectOne((req) => !!req.url.match(/api.agify.io/)) | ||
.flush({ ...defaultResponse, age: 33 }); | ||
|
||
expect(component.searchForm.get('name')?.value).toEqual('dennis'); | ||
expect(agifyServiceGetSpy).toHaveBeenCalledWith('dennis'); | ||
expect(await screen.findByText('33')).toBeTruthy(); | ||
|
||
screen.logTestingPlaygroundURL(); | ||
}); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters