Skip to content

Commit

Permalink
🟢 Update tests to have code coverage in all green on all metrics, 97%+
Browse files Browse the repository at this point in the history
98.86% Statements 87/88 100% Branches 13/13 97.14% Functions 34/35 98.83% Lines 85/86
  • Loading branch information
RonanCodes committed Apr 30, 2024
1 parent b496849 commit 5d382fd
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
import { TestBed } from '@angular/core/testing';

import { ChuckNorrisJokeGeneratorService } from './chuck-norris-joke-generator.service';
import { provideHttpClient } from '@angular/common/http';
import { HttpClient, provideHttpClient } from '@angular/common/http';
import { of } from 'rxjs';
import { ChuckNorrisJoke } from './chuck-norris-joke-generator.model';

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

let httpClient: jasmine.SpyObj<HttpClient>;

beforeEach(() => {
let httpClient = jasmine.createSpyObj('HttpClient', ['get']);
httpClient.get.and.returnValue(
of({ value: 'God prays to Chuck Norris' } as ChuckNorrisJoke)
);

TestBed.configureTestingModule({
providers: [provideHttpClient()],
providers: [{ provide: HttpClient, useValue: httpClient }],
});
service = TestBed.inject(ChuckNorrisJokeGeneratorService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

describe('#getJoke()', () => {
it('should return an Observable<Joke>', (done) => {
// Act
service.getJoke().subscribe((joke) => {
// Assert
expect(joke.value).toBe('God prays to Chuck Norris');
expect(joke.isFavourite).toBeFalse();
done();
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,11 @@ describe('FavouritesStoreService', () => {
});

describe('#toggleFavourite()', () => {
it('should update the #isFavourite property', (done) => {
it('should remove the joke', (done) => {
// Act
service.toggleFavourite(originalJoke);

service.favourites$.subscribe((favourites) => {
console.log(favourites);
expect(favourites.length).toBe(0);
done();
});
Expand All @@ -122,5 +121,22 @@ describe('FavouritesStoreService', () => {
JSON.stringify([])
);
});

it('should add the joke', (done) => {
// Act
service.toggleFavourite(newJoke);

service.favourites$.subscribe((favourites) => {
expect(favourites.length).toBe(2);
done();
});

// Assert
expect(originalJoke.isFavourite).toBe(true);
expect(localStorageService.saveToLocalStorage).toHaveBeenCalledWith(
localStorageKey.favourites,
JSON.stringify([originalJoke, newJoke])
);
});
});
});
28 changes: 21 additions & 7 deletions src/app/shared/data/store/joke-store/joke-store.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,25 @@ describe('JokeStoreService', () => {
});
});

// describe('#triggerNewJoke()', () => {
// it('should call the Chuck norris API 10 times, and emit on the store', () => {
// // Arrange
// // Act
// // Assert
// });
// });
describe('#triggerNewJoke()', () => {
it('should call the Chuck norris API 10 times, and emit on the store', (done) => {
// Arrange
const newJoke = {
value: `Chuck Norris can lick his elbows.`,
isFavourite: false,
};
chuckNorrisJokeGeneratorService.getJoke.and.returnValue(of(newJoke));

// Act
service.triggerNewJoke();

service.jokes$.subscribe((jokes) => {
expect(jokes[0]).toBe(newJoke);
done();
});

// Assert
expect(chuckNorrisJokeGeneratorService.getJoke).toHaveBeenCalledWith();
});
});
});

0 comments on commit 5d382fd

Please sign in to comment.