-
Notifications
You must be signed in to change notification settings - Fork 813
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to boost score api package (#31084)
* Add tests to boost-score-api package * changelog * Update package version * Update tests to not reassign values to global mockData
- Loading branch information
1 parent
16fb674
commit 180b1ee
Showing
4 changed files
with
98 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 4 additions & 0 deletions
4
projects/js-packages/boost-score-api/changelog/add-tests-to-boost-score-api-package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: added | ||
|
||
Add tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} ); | ||
} ); |