Skip to content

Commit

Permalink
test(app): enhanced coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bartholomej committed Apr 1, 2023
1 parent 806010d commit 7937e39
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
20 changes: 15 additions & 5 deletions src/helpers/movie.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
CSFDVod,
CSFDVodService
} from '../interfaces/movie.interface';
import { addProtocol, getColor, parseIdFromUrl, parseISO8601Duration } from './global.helper';
import { addProtocol, getColor, parseISO8601Duration, parseIdFromUrl } from './global.helper';

export const getId = (el: HTMLElement): number => {
const url = el.querySelector('.tabs .tab-nav-list a').attributes.href;
Expand Down Expand Up @@ -39,9 +39,11 @@ export const getColorRating = (bodyClasses: string[]): CSFDColorRating => {

export const getRating = (el: HTMLElement): number => {
const ratingRaw = el.querySelector('.film-rating-average').textContent;
const rating = +ratingRaw?.replace(/%/g, '').trim();
if (Number.isInteger(rating)) {
return rating;
const rating = ratingRaw?.replace(/%/g, '').trim();
const ratingInt = parseInt(rating);

if (Number.isInteger(ratingInt)) {
return ratingInt;
} else {
return null;
}
Expand Down Expand Up @@ -95,9 +97,15 @@ export const getDuration = (jsonLdRaw: string, el: HTMLElement): number => {

export const getTitlesOther = (el: HTMLElement): CSFDTitlesOther[] => {
const namesNode = el.querySelectorAll('.film-names li');
return namesNode.map((el) => {

if (!namesNode.length) {
return [];
}

const titlesOther = namesNode.map((el) => {
const country = el.querySelector('img.flag').attributes.alt;
const title = el.textContent.trim().split('\n')[0];

if (country && title) {
return {
country,
Expand All @@ -107,6 +115,8 @@ export const getTitlesOther = (el: HTMLElement): CSFDTitlesOther[] => {
return null;
}
});

return titlesOther.filter((x) => x);
};

export const getPoster = (el: HTMLElement | null): string => {
Expand Down
6 changes: 5 additions & 1 deletion tests/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ describe('Parse color', () => {
const url = parseColor('blue');
expect(url).toBe('average');
});
test('Bad', () => {
test('Grey', () => {
const url = parseColor('grey');
expect(url).toBe('bad');
});
test('Wrong color', () => {
const url = parseColor('adas' as any);
expect(url).toBe('unknown');
});
Expand Down
21 changes: 20 additions & 1 deletion tests/movie.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ const {
jsonLd: movieJsonLd
} = getMovie(movieHtml);

// Wrong html
const wrongHtml = parse(
'<div class="film-rating-average"></div><ul class="film-names"><li><img class="flag" /></li></ul>'
);

// Movie blank
const movieHtmlBlank = parse(movieMockBlank);

Expand Down Expand Up @@ -384,6 +389,10 @@ describe('Get year', () => {
const movie = getYear(seriesJsonLd);
expect(movie).toEqual<number>(1994);
});
test('Wrong year', () => {
const movie = getYear(null as any);
expect(movie).toEqual(null);
});
});

describe('Get rating count', () => {
Expand Down Expand Up @@ -581,7 +590,17 @@ describe('Get people', () => {
describe('Anomaly detection', () => {
test('Bad node for rating', () => {
const movie = getRatingCount(movieNode);
expect(movie).toEqual<CSFDMovieListItem[]>(null);
expect(movie).toEqual<CSFDMovieListItem[]>(null as any);
});

test('Wrong rating', () => {
const movie = getRating(wrongHtml);
expect(movie).toEqual(null);
});

test('Wrong otherTitle', () => {
const movie = getTitlesOther(wrongHtml);
expect(movie).toEqual([]);
});
});
});
18 changes: 16 additions & 2 deletions tests/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,31 @@ import { UserRatingsScraper } from '../src/services/user-ratings.service';

// Live API tests
const USER = 912;
const USER2 = 228645;

describe('Simple call', () => {
// Fetch data with excludes
const userRatingsScraper = new UserRatingsScraper();
const res: Promise<CSFDUserRatings[]> = userRatingsScraper.userRatings(USER);

test('Should have at least one film', async () => {
test('Should have some movies', async () => {
const results = await res;

const films = results.filter((item) => item.type === 'film');
expect(films.length).toBeGreaterThan(1);
expect(films.length).toBeGreaterThan(10);
});
});

describe('AllPages', () => {
const userRatingsScraper = new UserRatingsScraper();
const res: Promise<CSFDUserRatings[]> = userRatingsScraper.userRatings(USER2, {
allPages: true,
allPagesDelay: 100
});

test('Should have exact number of movies', async () => {
const results = await res;
expect(results.length).toBeCloseTo(181);
});
});

Expand Down

0 comments on commit 7937e39

Please sign in to comment.