Skip to content

Commit

Permalink
Fix an issue with ghost graphs, additional snapshot test images
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciopoppe committed Dec 15, 2023
1 parent 2e50501 commit 019e345
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 11 deletions.
40 changes: 35 additions & 5 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@ on:
types: [opened, reopened, synchronize]

jobs:
build-and-test:
build:
runs-on: ubuntu-latest

env:
FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY }}
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- uses: actions/[email protected]

- name: Install dependencies
run: npm install

- name: Build
run: npm run build

test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
Expand All @@ -25,11 +38,28 @@ jobs:
- name: Test
run: npm run test

- name: Build
run: npm run build
- name: Archive test artifacst
uses: actions/upload-artifact@v3
with:
name: test images
path: |
test/e2e/**/*.png
license:
runs-on: ubuntu-latest

env:
FOSSA_API_KEY: ${{ secrets.FOSSA_API_KEY }}

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Run Fossa and upload data
if: env.FOSSA_API_KEY != ''
uses: fossa-contrib/fossa-action@v1
with:
fossa-api-key: ${{ env.FOSSA_API_KEY }}

7 changes: 7 additions & 0 deletions site/jest-function-plot.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
<meta charset="UTF-8">
<title>Function Plot</title>
<link rel="stylesheet" type="text/css" rel="noopener" target="_blank" href="./css/reset.css">
<style>
body, text {
font-family: Arial, sans-serif;
color: black;
font-size: 14px;
}
</style>
</head>
<body>
<div id="playground"></div>
Expand Down
7 changes: 6 additions & 1 deletion src/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,9 @@ export class Chart extends EventEmitter.EventEmitter {
const graphs = content
.merge(contentEnter)
.selectAll(':scope > g.graph')
.data((d: FunctionPlotOptions) => d.data.map(datumDefaults))
.data((d: FunctionPlotOptions) => {
return d.data.map(datumDefaults)
})

// enter
const graphsEnter = graphs.enter().append('g').attr('class', 'graph')
Expand All @@ -548,6 +550,9 @@ export class Chart extends EventEmitter.EventEmitter {
selection.call(globals.graphTypes[d.graphType](self))
selection.call(helpers(self))
})

// exit
graphs.exit().remove()
}

buildZoomHelper() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
175 changes: 170 additions & 5 deletions test/e2e/graphs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@ import { toMatchImageSnapshot } from 'jest-image-snapshot'

expect.extend({ toMatchImageSnapshot })

const matchSnapshotConfig = {
comparisonMethod: 'ssim',
failureThreshold: 0.01,
failureThresholdType: 'percent'
}

describe('Function Plot', () => {
let browser
let page

beforeAll(async () => {
browser = await puppeteer.launch({ headless: 'new' })
page = await browser.newPage()
await page.setViewport({
width: 1000,
height: 1000,
deviceScaleFactor: 2
})
await page.goto('http://localhost:4444/jest-function-plot.html')
})

it('should render an x^2 graph', async () => {
await page.goto('http://localhost:4444/jest-function-plot.html')
await page.evaluate(`
functionPlot({
target: '#playground',
Expand All @@ -23,9 +34,163 @@ functionPlot({
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot({
failureThreshold: 0.01,
failureThresholdType: 'percent'
})
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should render with additional options', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
title: 'quadratic with options',
width: 580,
height: 400,
disableZoom: true,
xAxis: {
label: 'x - axis',
domain: [-6, 6]
},
yAxis: {
label: 'y - axis'
},
data: [{
fn: 'x^2'
}]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should render a grid', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
xAxis: {
label: 'real'
},
yAxis: {
label: 'imaginary'
},
grid: true,
data: [
{ fn: 'sqrt(1 - x * x)' },
{ fn: '-sqrt(1 - x * x)' }
]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should render distinct domains', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
yAxis: {domain: [-1, 1]},
xAxis: {domain: [8, 24]},
data: [{
fn: 'sin(x)'
}]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should evaluate with multiple samples', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
data: [{
fn: 'sin(x)',
nSamples: 1000
}]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should render annotations', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
yAxis: {domain: [-1, 9]},
data: [{
fn: 'x^2'
}],
annotations: [{
x: -1
}, {
x: 1,
text: 'x = 1'
}, {
y: 2,
text: 'y = 2'
}]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should render closed paths', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
xAxis: {domain: [-2, 12]},
data: [{
fn: '3 + sin(x)',
range: [2, 8],
closed: true
}]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should render logarithmic scales', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
xAxis: {
type: 'log',
domain: [0.01, 1]
},
yAxis: {
domain: [-100, 100]
},
grid: true,
data: [{
fn: '1/x * cos(1/x)',
// to make it look like a definite integral
closed: true
}]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})

it('should render different graph types', async () => {
await page.evaluate(`
functionPlot({
target: '#playground',
data: [{
fn: '-sqrt(-x)',
nSamples: 100,
graphType: 'scatter'
}, {
fn: 'sqrt(x)',
graphType: 'polyline'
}, {
fn: 'x^2',
graphType: 'interval'
}]
})
`)
const image = await page.screenshot()
expect(image).toMatchImageSnapshot(matchSnapshotConfig)
})
})

0 comments on commit 019e345

Please sign in to comment.