Skip to content

Commit

Permalink
Update tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
RonanCodes committed Apr 29, 2024
1 parent b37f41e commit a5d5936
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 29 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"ng-mocks": "^14.12.2",
"typescript": "~5.4.2"
}
}
}
15 changes: 0 additions & 15 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,4 @@ describe('AppComponent', () => {
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});

it(`should have the 'chuck-norris-joke-generator' title`, () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('chuck-norris-joke-generator');
});

it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('h1')?.textContent).toContain(
'Hello, chuck-norris-joke-generator',
);
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { TestBed } from '@angular/core/testing';

import { ChuckNorrisJokeGeneratorService } from './chuck-norris-joke-generator.service';
import { provideHttpClient } from '@angular/common/http';

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

beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
providers: [provideHttpClient()],
});
service = TestBed.inject(ChuckNorrisJokeGeneratorService);
});

Expand Down
10 changes: 6 additions & 4 deletions src/app/home/home.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { HomeComponent } from './home.component';
import { JokesComponent } from '../jokes/jokes.component';

import { MockComponent } from 'ng-mocks';

describe('HomeComponent', () => {
let component: HomeComponent;
let fixture: ComponentFixture<HomeComponent>;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [HomeComponent]
})
.compileComponents();

imports: [HomeComponent, MockComponent(JokesComponent)],
}).compileComponents();

fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
Expand Down
5 changes: 4 additions & 1 deletion src/app/joke-store/joke-store.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { TestBed } from '@angular/core/testing';

import { JokeStoreService } from './joke-store.service';
import { ChuckNorrisJokeGeneratorService } from '../chuck-norris-joke-generator/chuck-norris-joke-generator.service';

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

beforeEach(() => {
TestBed.configureTestingModule({});
TestBed.configureTestingModule({
providers: [{ provide: ChuckNorrisJokeGeneratorService, useValue: {} }],
});
service = TestBed.inject(JokeStoreService);
});

Expand Down
32 changes: 28 additions & 4 deletions src/app/jokes/jokes.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { ComponentFixture, TestBed } from '@angular/core/testing';

import { JokesComponent } from './jokes.component';
import { JokeStoreService } from '../joke-store/joke-store.service';
import { of } from 'rxjs';
import { CommonModule } from '@angular/common';

describe('JokesComponent', () => {
let component: JokesComponent;
let fixture: ComponentFixture<JokesComponent>;

beforeEach(async () => {
const jokesStoreService = jasmine.createSpyObj('JokeStoreService', [
'getJokes',
]);
jokesStoreService.getJokes.and.returnValue(
of(['Chuck Norris can divide by zero.'])
);

await TestBed.configureTestingModule({
imports: [JokesComponent]
})
.compileComponents();

imports: [JokesComponent, CommonModule],
providers: [
{
provide: JokeStoreService,
useValue: jokesStoreService,
},
],
}).compileComponents();

fixture = TestBed.createComponent(JokesComponent);
component = fixture.componentInstance;
fixture.detectChanges();
Expand All @@ -20,4 +35,13 @@ describe('JokesComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should render title', () => {
const fixture = TestBed.createComponent(JokesComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement as HTMLElement;
expect(compiled.querySelector('li')?.textContent).toContain(
'Chuck Norris can divide by zero.'
);
});
});
4 changes: 1 addition & 3 deletions src/app/jokes/jokes.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export class JokesComponent {

constructor(private jokeStoreService: JokeStoreService) {
// TODO: Look into using async pipe?
this.jokeStoreService.getJokes().subscribe((jokes) => {
this.jokes = jokes;
});
this.jokeStoreService.getJokes().subscribe((jokes) => (this.jokes = jokes));
}
}

0 comments on commit a5d5936

Please sign in to comment.