Skip to content

Commit

Permalink
tidy(web): Move test assertions over to jest style (#7766)
Browse files Browse the repository at this point in the history
* Fix #7761

* patch up types for tsconfig.node

* patch up types for tsconfig.node once more

* buff out one more chai style assertion

* continue using chai expect in cypress tests

* drop eslint-disable
  • Loading branch information
wegry authored Jan 28, 2025
1 parent fa676ac commit a31f496
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 195 deletions.
1 change: 1 addition & 0 deletions web/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"devDependencies": [
"cypress.config.ts",
"vite.config.ts",
"scripts/**/*.test.ts",
"src/testing/setupTests.ts",
"src/testing/testUtils.tsx",
"src/testing/mocks/**",
Expand Down
6 changes: 4 additions & 2 deletions web/geo/validate.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { GEO_CONFIG } from './generateWorld';
import { WorldFeatureCollection } from './types';
import { zeroOverlaps } from './validate';
Expand Down Expand Up @@ -169,7 +171,7 @@ const mockFeatureCollection: WorldFeatureCollection = {
describe('zeroOverlaps', () => {
it('should not throw if there are no overlaps', () => {
// assert that function does not throw error
expect(() => zeroOverlaps(mockFeatureCollection, GEO_CONFIG)).not.to.throw();
expect(() => zeroOverlaps(mockFeatureCollection, GEO_CONFIG)).not.toThrow();
});

it('should throw on overlaps', () => {
Expand All @@ -179,7 +181,7 @@ describe('zeroOverlaps', () => {
overlappingFeatureCollection.features[1].geometry.coordinates[1][0][0][0] = 11.0754;
overlappingFeatureCollection.features[1].geometry.coordinates[1][0][8][0] = 11.0754;
// assert that function throws error with specific message
expect(() => zeroOverlaps(overlappingFeatureCollection, GEO_CONFIG)).to.throw(
expect(() => zeroOverlaps(overlappingFeatureCollection, GEO_CONFIG)).toThrow(
'SE-SE3 overlaps with SE-SE4'
);
});
Expand Down
8 changes: 5 additions & 3 deletions web/scripts/generateZonesConfig.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import { getConfig } from './generateZonesConfig';

describe('generateZonesConfig', () => {
Expand All @@ -10,10 +12,10 @@ describe('generateZonesConfig', () => {
const indexOfContributor = result.contributors.indexOf(contributorName);

// Person should be in the list of contributors
expect(result.contributors).to.contain(contributorName);
expect(result.contributors).toContain(contributorName);
// Person should be in the list of contributors for the zone
expect(result.zones['DK-DK2'].contributors).to.contain(indexOfContributor);
expect(result.zones['DK-DK2'].contributors).toContain(indexOfContributor);
// Person should be in the list of contributors for the aggregated country
expect(result.zones['DK'].contributors).to.contain(indexOfContributor);
expect(result.zones['DK'].contributors).toContain(indexOfContributor);
});
});
4 changes: 2 additions & 2 deletions web/src/components/TimeSlider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { COLORS, getThumbIcon, getTrackBackground } from './TimeSlider';

describe('getTrackBackground', () => {
it('returns the day color when no sets are provided', () => {
expect(getTrackBackground(false, 10)).to.eq(COLORS.LIGHT_DAY);
expect(getTrackBackground(true, 10)).to.eq(COLORS.DARK_DAY);
expect(getTrackBackground(false, 10)).toEqual(COLORS.LIGHT_DAY);
expect(getTrackBackground(true, 10)).toEqual(COLORS.DARK_DAY);
});

it('returns a linear gradient with night time sets when sets are provided', () => {
Expand Down
46 changes: 24 additions & 22 deletions web/src/features/charts/bar-breakdown/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect, it } from 'vitest';

import {
convertPrice,
ExchangeDataType,
Expand Down Expand Up @@ -229,7 +231,7 @@ describe('getProductionData', () => {
it('returns correct data', () => {
const result = getProductionData(zoneDetailsData);
// TODO: Match snapshot
expect(result).to.deep.eq(productionData);
expect(result).toEqual(productionData);
});
});

Expand All @@ -241,7 +243,7 @@ describe('getElectricityProductionValue', () => {
production: 500,
storage: 0,
});
expect(result).to.eq(500);
expect(result).toEqual(500);
});
it('handles missing production value with zero capacity', () => {
const result = getElectricityProductionValue({
Expand All @@ -250,7 +252,7 @@ describe('getElectricityProductionValue', () => {
production: null,
storage: 0,
});
expect(result).to.eq(0);
expect(result).toEqual(0);
});

it('handles missing production value', () => {
Expand All @@ -261,7 +263,7 @@ describe('getElectricityProductionValue', () => {
storage: 0,
});

expect(result).to.eq(null);
expect(result).toEqual(null);
});
it('handles storage', () => {
const result = getElectricityProductionValue({
Expand All @@ -270,7 +272,7 @@ describe('getElectricityProductionValue', () => {
production: null,
storage: 300,
});
expect(result).to.eq(-300);
expect(result).toEqual(-300);
});
it('handles zero storage', () => {
const result = getElectricityProductionValue({
Expand All @@ -279,7 +281,7 @@ describe('getElectricityProductionValue', () => {
production: null,
storage: 0,
});
expect(result).to.eq(0);
expect(result).toEqual(0);
});
it('handles missing storage', () => {
const result = getElectricityProductionValue({
Expand All @@ -288,14 +290,14 @@ describe('getElectricityProductionValue', () => {
production: null,
storage: null,
});
expect(result).to.eq(null);
expect(result).toEqual(null);
});
});

describe('getDataBlockPositions', () => {
it('returns correct data', () => {
const result = getDataBlockPositions(productionData.length, exchangeData);
expect(result).to.deep.eq({
expect(result).toEqual({
exchangeFlagX: 50,
exchangeHeight: 40,
exchangeY: 262,
Expand All @@ -314,7 +316,7 @@ describe('getExchangesToDisplay', () => {
},
};
const result = getExchangesToDisplay(true, 'DE', ZoneStates);
expect(result).to.deep.eq(['AT', 'BE', 'NO']);
expect(result).toEqual(['AT', 'BE', 'NO']);
});
it('shows non-aggregated exchanges only when required', () => {
const ZoneStates = {
Expand All @@ -324,7 +326,7 @@ describe('getExchangesToDisplay', () => {
},
};
const result = getExchangesToDisplay(false, 'DE', ZoneStates);
expect(result).to.deep.eq(['AT', 'BE', 'NO-NO2']);
expect(result).toEqual(['AT', 'BE', 'NO-NO2']);
});
it('handles empty exchange', () => {
const ZoneStates = {
Expand All @@ -334,7 +336,7 @@ describe('getExchangesToDisplay', () => {
},
};
const result = getExchangesToDisplay(false, 'DE', ZoneStates);
expect(result).to.deep.eq([]);
expect(result).toEqual([]);
});
});

Expand All @@ -348,7 +350,7 @@ describe('getExchangeData', () => {
};
const result = getExchangeData(['ES', 'AT'], true, exchangeCapacitiesZoneDetailsData);

expect(result).to.deep.eq([
expect(result).toEqual([
{
exchange: 934,
exchangeCapacityRange: [-1000, 1000],
Expand All @@ -371,7 +373,7 @@ describe('getExchangeData', () => {
};
const result = getExchangeData(['ES'], true, exchangeCapacitiesZoneDetailsData);

expect(result).to.deep.eq([
expect(result).toEqual([
{
exchange: -934,
exchangeCapacityRange: [0, 0],
Expand All @@ -390,7 +392,7 @@ describe('getExchangeData', () => {
};
const result = getExchangeData(['ES'], true, exchangeCapacitiesZoneDetailsData);

expect(result).to.deep.equal([
expect(result).toEqual([
{
exchange: undefined,
exchangeCapacityRange: [0, 0],
Expand All @@ -411,7 +413,7 @@ describe('getExchangeCo2Intensity', () => {
};

const result = getExchangeCo2Intensity('ES', exchangeCapacitiesZoneDetailsData, true);
expect(result).to.eq(999);
expect(result).toEqual(999);
});
describe('when exchange value is less than 0', () => {
it('returns Co2 insensity when in Consumption mode', () => {
Expand All @@ -426,7 +428,7 @@ describe('getExchangeCo2Intensity', () => {
exchangeCapacitiesZoneDetailsData,
true
);
expect(result).to.eq(187.32);
expect(result).toEqual(187.32);
});
it('returns Co2 insensity production when in Production mode', () => {
const exchangeCapacitiesZoneDetailsData = {
Expand All @@ -440,34 +442,34 @@ describe('getExchangeCo2Intensity', () => {
exchangeCapacitiesZoneDetailsData,
false
);
expect(result).to.eq(190.6);
expect(result).toEqual(190.6);
});
});
});

describe('convertPrice', () => {
it('dont convert USD to price/KWh', () => {
const result = convertPrice(120, 'USD');
expect(result).to.deep.eq({ value: 120, currency: 'USD', unit: 'MWh' });
expect(result).toEqual({ value: 120, currency: 'USD', unit: 'MWh' });
});

it('handles missing currency', () => {
const result = convertPrice(120, undefined);
expect(result).to.deep.eq({ value: 120, currency: undefined, unit: 'MWh' });
expect(result).toEqual({ value: 120, currency: undefined, unit: 'MWh' });
});

it('handles missing price with EUR', () => {
const result = convertPrice(undefined, 'EUR');
expect(result).to.deep.eq({ value: undefined, currency: 'EUR', unit: 'MWh' });
expect(result).toEqual({ value: undefined, currency: 'EUR', unit: 'MWh' });
});

it('handles missing price without EUR', () => {
const result = convertPrice(undefined, 'USD');
expect(result).to.deep.eq({ value: undefined, currency: 'USD', unit: 'MWh' });
expect(result).toEqual({ value: undefined, currency: 'USD', unit: 'MWh' });
});

it('handles missing price and currency', () => {
const result = convertPrice(undefined, undefined);
expect(result).to.deep.eq({ value: undefined, currency: undefined, unit: 'MWh' });
expect(result).toEqual({ value: undefined, currency: undefined, unit: 'MWh' });
});
});
34 changes: 18 additions & 16 deletions web/src/features/charts/futurePriceUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { describe, expect } from 'vitest';

import {
calculatePriceBound,
dateIsFirstHourOfTomorrow,
Expand All @@ -13,28 +15,28 @@ describe('FuturePrice Utility Functions', () => {
const date = new Date('2024-09-02T03:35:00Z');
const granularity = 30;
const normalizedDate = normalizeToGranularity(date, granularity);
expect(normalizedDate.getMinutes()).to.equal(30);
expect(normalizedDate.getSeconds()).to.equal(0);
expect(normalizedDate.getMinutes()).toEqual(30);
expect(normalizedDate.getSeconds()).toEqual(0);
});

test('normalizeToGranularity should round down to hours', () => {
const date = new Date('2024-09-02T03:35:00Z');
const granularity = 60;
const normalizedDate = normalizeToGranularity(date, granularity);
expect(normalizedDate.getMinutes()).to.equal(0);
expect(normalizedDate.getSeconds()).to.equal(0);
expect(normalizedDate.getMinutes()).toEqual(0);
expect(normalizedDate.getSeconds()).toEqual(0);
});

test('dateIsFirstHourOfTomorrow returns true only is the first hour of the next day', () => {
const tomorrow = new Date();
tomorrow.setHours(0, 0, 0, 0);
tomorrow.setDate(tomorrow.getDate() + 1);
expect(dateIsFirstHourOfTomorrow(tomorrow)).to.equal(true);
expect(dateIsFirstHourOfTomorrow(tomorrow)).toEqual(true);

const tomorrowPlusHalfAnHour = new Date();
tomorrowPlusHalfAnHour.setHours(0, 30, 0, 0);
tomorrowPlusHalfAnHour.setDate(tomorrow.getDate() + 1);
expect(dateIsFirstHourOfTomorrow(tomorrowPlusHalfAnHour)).to.equal(false);
expect(dateIsFirstHourOfTomorrow(tomorrowPlusHalfAnHour)).toEqual(false);
});

test('filterPriceData filters out the dates that doesnt match the granularity', () => {
Expand All @@ -45,7 +47,7 @@ describe('FuturePrice Utility Functions', () => {
};
const granularity = 60;
const filteredData = filterPriceData(priceData, granularity);
expect(Object.keys(filteredData).length).to.equal(2);
expect(Object.keys(filteredData).length).toEqual(2);
});

test('getGranularity returns time granularity of the data', () => {
Expand All @@ -55,7 +57,7 @@ describe('FuturePrice Utility Functions', () => {
'2024-09-02T04:00:00Z': 30,
};
const granularity = getGranularity(priceData);
expect(granularity).to.equal(30);
expect(granularity).toEqual(30);
});

test('calculatePriceBound returns highest and lowest price in dataset', () => {
Expand All @@ -67,24 +69,24 @@ describe('FuturePrice Utility Functions', () => {

const granularity = 15;
const minPrice = calculatePriceBound(priceData, Math.min, granularity);
expect(minPrice).to.equal(-10);
expect(minPrice).toEqual(-10);

const maxPrice = calculatePriceBound(priceData, Math.max, granularity);
expect(maxPrice).to.equal(30);
expect(maxPrice).toEqual(30);
});

test('negativeToPostivePercentage returns the correct percentage of negative to postive ratio', () => {
const minPrice = -10;
const maxPrice = 20;
const percentage = negativeToPostivePercentage(minPrice, maxPrice);
expect(percentage).to.equal(33);
expect(percentage).toEqual(33);
});

test('negativeToPostivePercentage returns 0 if the min is above 0', () => {
const minPrice = 10;
const maxPrice = 20;
const percentage = negativeToPostivePercentage(minPrice, maxPrice);
expect(percentage).to.equal(0);
expect(percentage).toEqual(0);
});

test('priceIn5Percentile returns true if price is in top 5 percentile', () => {
Expand All @@ -93,7 +95,7 @@ describe('FuturePrice Utility Functions', () => {
const minPrice = 0;
const inTop = true;
const result = priceIn5Percentile(price, maxPrice, minPrice, inTop);
expect(result).to.equal(true);
expect(result).toEqual(true);
});

test('priceIn5Percentile returns false if price is not in top 5 percentile', () => {
Expand All @@ -102,7 +104,7 @@ describe('FuturePrice Utility Functions', () => {
const minPrice = 50;
const inTop = true;
const result = priceIn5Percentile(price, maxPrice, minPrice, inTop);
expect(result).to.equal(false);
expect(result).toEqual(false);
});

test('priceIn5Percentile returns true if price is in bottom 5 percentile', () => {
Expand All @@ -111,7 +113,7 @@ describe('FuturePrice Utility Functions', () => {
const minPrice = 50;
const inTop = false;
const result = priceIn5Percentile(price, maxPrice, minPrice, inTop);
expect(result).to.equal(true);
expect(result).toEqual(true);
});

test('priceIn5Percentile returns false if price is not in bottom 5 percentile', () => {
Expand All @@ -120,6 +122,6 @@ describe('FuturePrice Utility Functions', () => {
const minPrice = 50;
const inTop = false;
const result = priceIn5Percentile(price, maxPrice, minPrice, inTop);
expect(result).to.equal(false);
expect(result).toEqual(false);
});
});
Loading

0 comments on commit a31f496

Please sign in to comment.