-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: fit functions for null y1 values (#416)
Add ability to fit data of non-stacked line and area charts with a specified fit function type * add fit functions for null y1 values * allow end values to be explicitly set as fallback * add visual regression tests * add unit tests and better testing utils Note: Does not *yet* support stacked charts or `y0` values. This is a future enhancement is being tracked in #450. related to #388
- Loading branch information
1 parent
f2c7bd9
commit e083755
Showing
116 changed files
with
2,869 additions
and
155 deletions.
There are no files selected for viewing
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,38 +1,66 @@ | ||
import React from 'react'; | ||
import { | ||
Axis, | ||
Chart, | ||
getAxisId, | ||
getSpecId, | ||
Position, | ||
ScaleType, | ||
HistogramBarSeries, | ||
DARK_THEME, | ||
Settings, | ||
} from '../src'; | ||
import { KIBANA_METRICS } from '../src/utils/data_samples/test_dataset_kibana'; | ||
import { Axis, Chart, getAxisId, getSpecId, Position, ScaleType, Settings, LineSeries } from '../src'; | ||
import { Fit } from '../src/chart_types/xy_chart/utils/specs'; | ||
|
||
const data = [ | ||
{ x: 0, y: null }, | ||
{ x: 1, y: 3 }, | ||
{ x: 2, y: 5 }, | ||
{ x: 3, y: null }, | ||
{ x: 4, y: 4 }, | ||
{ x: 5, y: null }, | ||
{ x: 6, y: 5 }, | ||
{ x: 7, y: 6 }, | ||
{ x: 8, y: null }, | ||
{ x: 9, y: null }, | ||
{ x: 10, y: null }, | ||
{ x: 11, y: 12 }, | ||
{ x: 12, y: null }, | ||
]; | ||
|
||
export class Playground extends React.Component { | ||
render() { | ||
const data = KIBANA_METRICS.metrics.kibana_os_load[0].data.slice(0, 5); | ||
return ( | ||
<div className="chart"> | ||
<Chart> | ||
<Settings theme={DARK_THEME} rotation={180} /> | ||
<Axis id={getAxisId('x')} position={Position.Bottom} /> | ||
<Axis id={getAxisId('y')} position={Position.Left} /> | ||
|
||
<HistogramBarSeries | ||
id={getSpecId('series bars chart')} | ||
xScaleType={ScaleType.Linear} | ||
yScaleType={ScaleType.Linear} | ||
xAccessor={0} | ||
yAccessors={[1]} | ||
data={data} | ||
yScaleToDataExtent={true} | ||
/> | ||
</Chart> | ||
</div> | ||
<> | ||
<div className="chart"> | ||
<Chart className="story-chart"> | ||
<Settings | ||
showLegend | ||
theme={{ | ||
areaSeriesStyle: { | ||
point: { | ||
visible: true, | ||
}, | ||
}, | ||
}} | ||
/> | ||
<Axis | ||
id={getAxisId('bottom')} | ||
position={Position.Bottom} | ||
title={'Bottom axis'} | ||
showOverlappingTicks={true} | ||
/> | ||
<Axis id={getAxisId('left')} title={'Left axis'} position={Position.Left} /> | ||
<LineSeries | ||
id={getSpecId('test')} | ||
xScaleType={ScaleType.Linear} | ||
yScaleType={ScaleType.Linear} | ||
xAccessor={'x'} | ||
yAccessors={['y']} | ||
// curve={2} | ||
// splitSeriesAccessors={['g']} | ||
// stackAccessors={['x']} | ||
fit={Fit.Linear} | ||
data={data} | ||
// fit={{ | ||
// type: Fit.Average, | ||
// endValue: 0, | ||
// }} | ||
// data={data} | ||
/> | ||
</Chart> | ||
</div> | ||
</> | ||
); | ||
} | ||
} |
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,2 @@ | ||
// https://github.com/jest-community/jest-extended | ||
import 'jest-extended'; |
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
Binary file added
BIN
+11.7 KB
...ea-chart-fitting-functions-non-stacked-series-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+47.3 KB
...hart-stacked-with-separated-specs-same-naming-visually-looks-correct-2-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.5 KB
...d-charts-fitting-functions-non-stacked-series-visually-looks-correct-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.8 KB
...-bar-series-stories-test-histogram-mode-linear-point-alignment-start-2-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+18.1 KB
...harts-end-value-set-to-2-should-display-correct-fit-for-type-average-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.2 KB
...-charts-end-value-set-to-2-should-display-correct-fit-for-type-carry-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+19.6 KB
...arts-end-value-set-to-2-should-display-correct-fit-for-type-explicit-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+18.8 KB
...charts-end-value-set-to-2-should-display-correct-fit-for-type-linear-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+17 KB
...rts-end-value-set-to-2-should-display-correct-fit-for-type-lookahead-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.8 KB
...harts-end-value-set-to-2-should-display-correct-fit-for-type-nearest-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.1 KB
...a-charts-end-value-set-to-2-should-display-correct-fit-for-type-none-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.1 KB
...a-charts-end-value-set-to-2-should-display-correct-fit-for-type-zero-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.5 KB
...end-value-set-to-nearest-should-display-correct-fit-for-type-average-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.8 KB
...s-end-value-set-to-nearest-should-display-correct-fit-for-type-carry-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+19.6 KB
...nd-value-set-to-nearest-should-display-correct-fit-for-type-explicit-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.1 KB
...-end-value-set-to-nearest-should-display-correct-fit-for-type-linear-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.8 KB
...d-value-set-to-nearest-should-display-correct-fit-for-type-lookahead-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.8 KB
...end-value-set-to-nearest-should-display-correct-fit-for-type-nearest-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.1 KB
...ts-end-value-set-to-nearest-should-display-correct-fit-for-type-none-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+23.1 KB
...ts-end-value-set-to-nearest-should-display-correct-fit-for-type-zero-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+15.5 KB
...area-charts-no-end-value-should-display-correct-fit-for-type-average-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.8 KB
...s-area-charts-no-end-value-should-display-correct-fit-for-type-carry-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+19.6 KB
...rea-charts-no-end-value-should-display-correct-fit-for-type-explicit-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+16.1 KB
...-area-charts-no-end-value-should-display-correct-fit-for-type-linear-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.9 KB
...ea-charts-no-end-value-should-display-correct-fit-for-type-lookahead-1-snap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+14.8 KB
...area-charts-no-end-value-should-display-correct-fit-for-type-nearest-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+12.1 KB
...ns-area-charts-no-end-value-should-display-correct-fit-for-type-none-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+23.1 KB
...ns-area-charts-no-end-value-should-display-correct-fit-for-type-zero-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.6 KB
...nal-dataset-no-end-value-should-display-correct-fit-for-type-average-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.1 KB
...dinal-dataset-no-end-value-should-display-correct-fit-for-type-carry-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+19.1 KB
...al-dataset-no-end-value-should-display-correct-fit-for-type-explicit-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+15.1 KB
...inal-dataset-no-end-value-should-display-correct-fit-for-type-linear-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.1 KB
...l-dataset-no-end-value-should-display-correct-fit-for-type-lookahead-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.3 KB
...nal-dataset-no-end-value-should-display-correct-fit-for-type-nearest-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+11.4 KB
...rdinal-dataset-no-end-value-should-display-correct-fit-for-type-none-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+22.4 KB
...rdinal-dataset-no-end-value-should-display-correct-fit-for-type-zero-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+18.1 KB
...urved-end-value-set-to-2-should-display-correct-fit-for-type-average-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+15.2 KB
...-curved-end-value-set-to-2-should-display-correct-fit-for-type-carry-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+19.6 KB
...rved-end-value-set-to-2-should-display-correct-fit-for-type-explicit-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+18.8 KB
...curved-end-value-set-to-2-should-display-correct-fit-for-type-linear-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+17 KB
...ved-end-value-set-to-2-should-display-correct-fit-for-type-lookahead-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.8 KB
...urved-end-value-set-to-2-should-display-correct-fit-for-type-nearest-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+12.1 KB
...h-curved-end-value-set-to-2-should-display-correct-fit-for-type-none-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+23.1 KB
...h-curved-end-value-set-to-2-should-display-correct-fit-for-type-zero-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+16.7 KB
...harts-end-value-set-to-2-should-display-correct-fit-for-type-average-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.4 KB
...-charts-end-value-set-to-2-should-display-correct-fit-for-type-carry-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+18 KB
...arts-end-value-set-to-2-should-display-correct-fit-for-type-explicit-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+17.3 KB
...charts-end-value-set-to-2-should-display-correct-fit-for-type-linear-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+15.8 KB
...rts-end-value-set-to-2-should-display-correct-fit-for-type-lookahead-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+16.2 KB
...harts-end-value-set-to-2-should-display-correct-fit-for-type-nearest-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+12 KB
...e-charts-end-value-set-to-2-should-display-correct-fit-for-type-none-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+21.1 KB
...e-charts-end-value-set-to-2-should-display-correct-fit-for-type-zero-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.6 KB
...line-charts-no-end-value-should-display-correct-fit-for-type-average-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14 KB
...s-line-charts-no-end-value-should-display-correct-fit-for-type-carry-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+18 KB
...ine-charts-no-end-value-should-display-correct-fit-for-type-explicit-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+15.1 KB
...-line-charts-no-end-value-should-display-correct-fit-for-type-linear-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14 KB
...ne-charts-no-end-value-should-display-correct-fit-for-type-lookahead-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.1 KB
...line-charts-no-end-value-should-display-correct-fit-for-type-nearest-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+12 KB
...ns-line-charts-no-end-value-should-display-correct-fit-for-type-none-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+21.1 KB
...ns-line-charts-no-end-value-should-display-correct-fit-for-type-zero-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+16.7 KB
...curve-end-value-set-to-2-should-display-correct-fit-for-type-average-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+14.4 KB
...h-curve-end-value-set-to-2-should-display-correct-fit-for-type-carry-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+18 KB
...urve-end-value-set-to-2-should-display-correct-fit-for-type-explicit-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+17.3 KB
...-curve-end-value-set-to-2-should-display-correct-fit-for-type-linear-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+15.8 KB
...rve-end-value-set-to-2-should-display-correct-fit-for-type-lookahead-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+16.2 KB
...curve-end-value-set-to-2-should-display-correct-fit-for-type-nearest-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+12 KB
...th-curve-end-value-set-to-2-should-display-correct-fit-for-type-none-1-snap.png
Oops, something went wrong.
Binary file added
BIN
+21.1 KB
...th-curve-end-value-set-to-2-should-display-correct-fit-for-type-zero-1-snap.png
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { common } from '../page_objects'; | ||
import { Fit } from '../../src/chart_types/xy_chart/utils/specs'; | ||
|
||
describe('Mixed series stories', () => { | ||
describe('Fitting functions', () => { | ||
describe('Area charts - no endValue', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=area&knob-dataset=all&knob-fitting function=${fitType}&knob-Curve=0&knob-End value=none&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Area charts - endValue set to 2', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=area&knob-dataset=all&knob-fitting function=${fitType}&knob-Curve=0&knob-End value=2&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Area charts - endValue set to "nearest"', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=area&knob-dataset=all&knob-fitting function=${fitType}&knob-Curve=0&knob-End value=nearest&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Area charts - with curved - endValue set to 2', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=area&knob-dataset=all&knob-fitting function=${fitType}&knob-Curve=1&knob-End value=2&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Area charts - Ordinal dataset - no endValue', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=area&knob-dataset=ordinal&knob-fitting function=${fitType}&knob-Curve=0&knob-End value=none&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Line charts - no endValue', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=line&knob-dataset=all&knob-fitting function=${fitType}&knob-Curve=0&knob-End value=none&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Line charts - endValue set to 2', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=line&knob-dataset=all&knob-fitting function=${fitType}&knob-Curve=0&knob-End value=2&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Line charts - with curve - endValue set to 2', () => { | ||
Object.values(Fit).forEach((fitType) => { | ||
it(`should display correct fit for type - ${fitType}`, async () => { | ||
await common.expectChartAtUrlToMatchScreenshot( | ||
`http://localhost:9001/?path=/story/mixed-charts--fitting-functions-non-stacked-series&knob-seriesType=line&knob-dataset=all&knob-fitting function=${fitType}&knob-Curve=1&knob-End value=2&knob-Explicit valuve (using Fit.Explicit)=8`, | ||
); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
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
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,70 @@ | ||
import { matcherErrorMessage } from 'jest-matcher-utils'; | ||
|
||
// ensure this is parsed as a module. | ||
export {}; | ||
|
||
/** | ||
* Final Matcher type with `this` and `received` args removed from jest matcher function | ||
*/ | ||
type MatcherParameters<T extends (this: any, received: any, ...args: any[]) => any> = T extends ( | ||
this: any, | ||
received: any, | ||
...args: infer P | ||
) => any | ||
? P | ||
: never; | ||
|
||
declare global { | ||
namespace jest { // eslint-disable-line | ||
interface Matchers<R> { | ||
/** | ||
* Expect array to be filled with value, and optionally length | ||
*/ | ||
toEqualArrayOf(...args: MatcherParameters<typeof toEqualArrayOf>): R; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Expect array to be filled with value, and optionally length | ||
*/ | ||
function toEqualArrayOf(this: jest.MatcherUtils, received: any[], value: any, length?: number) { | ||
const matcherName = 'toEqualArrayOf'; | ||
|
||
if (!Array.isArray(received)) { | ||
throw new Error( | ||
matcherErrorMessage( | ||
this.utils.matcherHint(matcherName), | ||
`${this.utils.RECEIVED_COLOR('received')} value must be an array.`, | ||
`Received type: ${typeof received}`, | ||
), | ||
); | ||
} | ||
|
||
const receivedPretty = this.utils.printReceived(received); | ||
const elementCheck = received.every((v) => v === value); | ||
const lengthCheck = length === undefined || received.length === length; | ||
|
||
if (!lengthCheck) { | ||
return { | ||
pass: false, | ||
message: () => `expected array length to be ${length} but got ${received.length}`, | ||
}; | ||
} | ||
|
||
if (!elementCheck) { | ||
return { | ||
pass: false, | ||
message: () => `expected ${receivedPretty} to be an array of ${value}'s`, | ||
}; | ||
} | ||
|
||
return { | ||
pass: true, | ||
message: () => `expected ${receivedPretty} not to be an array of ${value}'s`, | ||
}; | ||
} | ||
|
||
expect.extend({ | ||
toEqualArrayOf, | ||
}); |
Oops, something went wrong.