Skip to content

Commit

Permalink
Add tests to boost score api package (#31084)
Browse files Browse the repository at this point in the history
* Add tests to boost-score-api package

* changelog

* Update package version

* Update tests to not reassign values to global mockData
  • Loading branch information
CodeyGuyDylan authored Jun 1, 2023
1 parent 16fb674 commit 180b1ee
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add tests
8 changes: 6 additions & 2 deletions projects/js-packages/boost-score-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@automattic/jetpack-boost-score-api",
"version": "0.1.0",
"version": "0.1.1-alpha",
"description": "A package to get the Jetpack Boost score of a site",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/js-packages/boost-score-api/#readme",
"bugs": {
Expand Down Expand Up @@ -28,6 +28,7 @@
"@typescript-eslint/parser": "5.59.0",
"eslint": "8.39.0",
"jest": "29.5.0",
"jest-environment-jsdom": "29.4.3",
"ts-loader": "9.4.2",
"typescript": "5.0.4",
"webpack": "5.76.0",
Expand All @@ -45,5 +46,8 @@
}
},
"main": "./build/index.js",
"types": "./build/index.d.ts"
"types": "./build/index.d.ts",
"jest": {
"testEnvironment": "jsdom"
}
}
89 changes: 85 additions & 4 deletions projects/js-packages/boost-score-api/tests/index.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,89 @@
import { requestSpeedScores } from '../src/index';
import api from '../src/api';
import {
requestSpeedScores,
getScoreLetter,
didScoresChange,
getScoreMovementPercentage,
} from '../src/index';

const mockData = {
status: 'success',
timestamp: 123456789,
scores: {
current: {
desktop: 90,
mobile: 80,
},
noBoost: {
desktop: 90,
mobile: 80,
},
isStale: true,
},
theme: '',
};

// This is a placeholder test, new tests will be added soon
describe( 'requestSpeedScores', () => {
it( 'should be a function', () => {
expect( typeof requestSpeedScores ).toBe( 'function' );
beforeEach( () => {
jest.spyOn( api, 'post' );
} );

afterEach( () => {
jest.restoreAllMocks();
} );

it( 'should return speed scores', async () => {
api.post.mockResolvedValue( mockData );

const scores = await requestSpeedScores( 'https://example.com' );
expect( scores ).toEqual( mockData.scores );
} );
} );

describe( 'getScoreLetter', () => {
it( 'Should return the correct score', () => {
expect( getScoreLetter( 90, 91 ) ).toBe( 'A' );
expect( getScoreLetter( 90, 83 ) ).toBe( 'B' );
expect( getScoreLetter( 90, 60 ) ).toBe( 'C' );
expect( getScoreLetter( 45, 50 ) ).toBe( 'D' );
expect( getScoreLetter( 26, 30 ) ).toBe( 'E' );
expect( getScoreLetter( 0, 0 ) ).toBe( 'F' );
} );
} );

describe( 'didScoresChange', () => {
it( 'Should return false if scores did not change', () => {
expect( didScoresChange( mockData.scores ) ).toBe( false );
} );

it( 'should return true if scores changed', () => {
const changedMockData = Object.assign( {}, mockData );
changedMockData.scores.noBoost.desktop = 60;
changedMockData.scores.noBoost.mobile = 50;

expect( didScoresChange( changedMockData.scores ) ).toBe( true );
} );
} );

describe( 'getScoreMovementPercentage', () => {
it( 'returns the correct percentage of scores moved', () => {
const changedMockData = Object.assign( {}, mockData );
const newScores = {
current: {
desktop: 90,
mobile: 80,
},
noBoost: {
desktop: 90,
mobile: 80,
},
};
changedMockData.scores = newScores;

expect( getScoreMovementPercentage( changedMockData.scores ) ).toBe( 0 );

( changedMockData.scores.noBoost.desktop = 80 ), ( changedMockData.scores.noBoost.mobile = 70 );

expect( getScoreMovementPercentage( changedMockData.scores ) ).toBe( 13 );
} );
} );

0 comments on commit 180b1ee

Please sign in to comment.