test(helpers/getavgrating): full coverage unit tests

This commit is contained in:
Zane Schaffer 2022-07-19 13:17:46 -07:00
parent 2fbde8cf3f
commit 033ae2ffe0
7 changed files with 43 additions and 8 deletions

View File

@ -5,7 +5,7 @@ const config: PlaywrightTestConfig = {
command: 'npm run build && npm run preview',
port: 3000
},
testMatch: 'tests/**/*.js'
testMatch: 'tests/**/*.ts'
};
export default config;

4
setupTest.js Normal file
View File

@ -0,0 +1,4 @@
import matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';
expect.extend(matchers);

View File

@ -1,5 +1,5 @@
export const getAvgRating = (ratings) => {
if (!ratings) return 0;
if (!ratings) return 0;
if (ratings.length === 1) return ratings[0].rating;
else return ratings.reduce((prev, curr) => prev.rating + curr.rating) / ratings.length;
};

View File

@ -1,5 +1,35 @@
import { getAvgRating } from '$helpers/getAvgRating.js';
test('return false if no ratings are passed', async () => {
let testRating1 = {
_key: '5a80f4748d91',
comments: 'The new one seems pretty good but my old one died and got sticky.',
emotion: { _ref: '633f8d5f-897a-461f-a817-ee910f6ad614', _type: 'reference' },
name: 'xyn',
rating: 4
};
let testRating2 = {
_key: '5a80f4748d91',
comments: 'This is a fake comment for testing, wahoo',
emotion: { _ref: '633f8d5f-897a-461f-a817-ee910f6ad614', _type: 'reference' },
name: 'zane',
rating: 3
};
test('return false if no parameters are passed', async () => {
expect(getAvgRating()).toBeFalsy();
});
test('return false if array with empty object is passed', async () => {
expect(getAvgRating([{}])).toBeFalsy();
});
test('return single rating if only one rating is passed', async () => {
expect(getAvgRating([testRating1])).toBe(testRating1.rating);
});
test('return average of 2 ratings passed', async () => {
expect(getAvgRating([testRating1, testRating2])).toBe(
(testRating1.rating + testRating2.rating) / 2
);
});

View File

@ -1,6 +1,6 @@
<script>
import { productsView, products, tags } from '$lib/stores';
import {normalize, getAvgRating } from '$helpers';
import { normalize, getAvgRating } from '$helpers';
export let filters;
export let reset;
let { selectedCat, selectedRating } = filters;

View File

@ -1,6 +1,6 @@
import { expect, test } from '@playwright/test';
test('index page has expected h1', async ({ page }) => {
await page.goto('/');
expect(await page.textContent('main')).toBe('Welcome to SvelteKit');
});
// test('index page has expected h1', async ({ page }) => {
// await page.goto('/');
// expect(await page.textContent('main')).toBe('Welcome to SvelteKit');
// });

View File

@ -30,6 +30,7 @@ const config = {
coverage: {
exclude: ['setupTest.js', 'src/mocks']
},
setupFiles: ['./setupTest.js'],
deps: {
// Put Svelte component here, e.g., inline: [/svelte-multiselect/, /msw/]
},