Skip to content

Commit d784c2c

Browse files
committed
refactor tests
1 parent 854ca59 commit d784c2c

File tree

8 files changed

+201
-239
lines changed

8 files changed

+201
-239
lines changed

niivue/src/components/Menu.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import { Signal, computed, effect, useSignal } from '@preact/signals'
44
import { addImagesEvent, addOverlayEvent, openImageFromURL } from '../events'
55
import { SLICE_TYPE } from '@niivue/niivue'
66
import { ScalingBox } from './ScalingBox'
7-
import { getMetadataString } from '../utility'
8-
import { getNumberOfPoints } from '../utility'
7+
import { getMetadataString, getNumberOfPoints } from '../utility'
98
import {
109
HeaderDialog,
1110
ImageSelect,

niivue/src/events.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export function listenToMessages(appProps: AppProps) {
5252
}
5353

5454
function handleDebugMessage(body: any, appProps: AppProps) {
55+
// sending arrays is fine, dataBuffer does not work
56+
// sending objects only works with one element inside
5557
const { nvArray } = appProps
5658
switch (body) {
5759
case 'getNCanvas':
@@ -64,8 +66,6 @@ function handleDebugMessage(body: any, appProps: AppProps) {
6466
break
6567
case 'getMinMaxOfFirstImage':
6668
{
67-
console.log('nv.volumes:')
68-
console.log(nvArray.value[0].volumes[0])
6969
window.postMessage({
7070
type: 'debugAnswer',
7171
body: [nvArray.value[0].volumes[0].cal_min, nvArray.value[0].volumes[0].cal_max],
@@ -131,43 +131,37 @@ function addMeshOverlay(nv: Niivue, item: any, type: string) {
131131
mesh.layers.pop()
132132
}
133133
if (!item.data) {
134-
console.log('Running loadFromURL');
135134
NVMesh.loadLayer(
136-
{url: item.uri,
137-
opacity:a.opacity,
138-
colormap:a.colormap,
139-
colormapNegative:a.colormapNegative,
140-
useNegativeCmap:a.useNegativeCmap,
141-
cal_min:a.calMin,
142-
cal_max:a.calMax,
135+
{
136+
url: item.uri,
137+
opacity: a.opacity,
138+
colormap: a.colormap,
139+
colormapNegative: a.colormapNegative,
140+
useNegativeCmap: a.useNegativeCmap,
141+
cal_min: a.calMin,
142+
cal_max: a.calMax,
143143
},
144144
mesh,
145-
)
145+
)
146146
} else {
147-
console.log('Running readLayer');
148147
NVMesh.readLayer(
149-
item.uri,
150-
item.data,
151-
mesh,
152-
a.opacity,
153-
a.colormap,
154-
a.colormapNegative,
155-
a.useNegativeCmap,
156-
a.calMin,
157-
a.calMax,
148+
item.uri,
149+
item.data,
150+
mesh,
151+
a.opacity,
152+
a.colormap,
153+
a.colormapNegative,
154+
a.useNegativeCmap,
155+
a.calMin,
156+
a.calMax,
158157
)
159158
}
160159
mesh.updateMesh(nv.gl)
161160
nv.opts.isColorbar = true
162161
nv.updateGLVolume()
163162
const layerNumber = nv.meshes[0].layers.length - 1
164163
if (type === 'addMeshCurvature') {
165-
nv.setMeshLayerProperty(
166-
nv.meshes[0].id,
167-
layerNumber,
168-
'colorbarVisible',
169-
false,
170-
)
164+
nv.setMeshLayerProperty(nv.meshes[0].id, layerNumber, 'colorbarVisible', false)
171165
}
172166
}
173167

niivue/tests/Images.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { test, expect } from '@playwright/test'
2+
import { BASE_URL, loadTestImage } from './utils'
3+
4+
test.describe('Loading images', () => {
5+
test('loads a test image', async ({ page }) => {
6+
await page.goto(BASE_URL)
7+
8+
await loadTestImage(page)
9+
10+
expect(await page.waitForSelector('canvas')).toBeTruthy()
11+
expect(await page.$$('canvas')).toHaveLength(1)
12+
expect(
13+
await page.textContent('text=/matrix size: 207 x 256 x 215, voxelsize: 0.74 x 0.74 x 0.74/i'),
14+
).toBeTruthy()
15+
16+
await loadTestImage(page)
17+
expect(await page.$$('canvas')).toHaveLength(2)
18+
})
19+
})

niivue/tests/Menu.spec.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { test, expect } from '@playwright/test'
2+
import { BASE_URL, loadTestImage } from './utils'
3+
4+
test.describe('Menu', () => {
5+
test('displays home screen', async ({ page }) => {
6+
await page.goto(BASE_URL)
7+
8+
expect(await page.textContent('text=/Home/i')).toBeTruthy()
9+
expect(await page.textContent('text=/Add Image/i')).toBeTruthy()
10+
expect(await page.textContent('text=/View/i')).toBeTruthy()
11+
expect(await page.textContent('text=/Bookmarklet/i')).toBeTruthy()
12+
expect(await page.textContent('text=/Drop Files to load images/i')).toBeTruthy()
13+
})
14+
15+
test('menubar updates with loading images', async ({ page }) => {
16+
await page.goto(BASE_URL)
17+
18+
// initially only these menu items are visible
19+
expect(await page.textContent('text=/Home/i')).toBeTruthy()
20+
expect(await page.textContent('text=/Add Image/i')).toBeTruthy()
21+
expect(await page.textContent('text=/View/i')).toBeTruthy()
22+
23+
// initially these menu items do not exist
24+
expect(await page.$('text=/ColorScale/i')).toBeNull()
25+
expect(await page.$('text=/Overlay/i')).toBeNull()
26+
expect(await page.$('text=/Header/i')).toBeNull()
27+
expect(await page.$('text=/Select/i')).toBeNull()
28+
29+
// load an image
30+
await loadTestImage(page)
31+
expect(await page.waitForSelector('canvas')).toBeTruthy()
32+
expect(
33+
await page.textContent('text=/matrix size: 207 x 256 x 215, voxelsize: 0.74 x 0.74 x 0.74/i'),
34+
).toBeTruthy()
35+
36+
// after loading an image these are visible
37+
expect(await page.textContent('text=/Home/i')).toBeTruthy()
38+
expect(await page.textContent('text=/Add Image/i')).toBeTruthy()
39+
expect(await page.textContent('text=/View/i')).toBeTruthy()
40+
expect(await page.textContent('text=/ColorScale/i')).toBeTruthy()
41+
expect(await page.textContent('text=/Overlay/i')).toBeTruthy()
42+
expect(await page.textContent('text=/Header/i')).toBeTruthy()
43+
44+
// the select menu item is only visible after loading 2 images
45+
expect(await page.$('text=/Select/i')).toBeNull()
46+
await loadTestImage(page)
47+
expect(await page.textContent('text=/Select/i')).toBeTruthy()
48+
})
49+
50+
test('loads an image and checks the menu bar', async ({ page }) => {
51+
await page.goto(BASE_URL)
52+
53+
await loadTestImage(page)
54+
55+
expect(await page.waitForSelector('canvas')).toBeTruthy()
56+
expect(await page.$$('canvas')).toHaveLength(1)
57+
expect(
58+
await page.textContent('text=/matrix size: 207 x 256 x 215, voxelsize: 0.74 x 0.74 x 0.74/i'),
59+
).toBeTruthy()
60+
61+
const menuBar = ['Home', 'Add Image', 'View', 'ColorScale', 'Overlay', 'Header']
62+
for (const item of menuBar) {
63+
expect(await page.textContent(`text=/${item}/i`)).toBeTruthy()
64+
}
65+
})
66+
67+
test('opens the example image via the menu bar', async ({ page }) => {
68+
await page.goto(BASE_URL)
69+
70+
await page.click('data-testid=menu-item-dropdown-Add Image')
71+
await page.click('text=/Example Image/i')
72+
73+
expect(await page.waitForSelector('canvas')).toBeTruthy()
74+
expect(await page.$$('canvas')).toHaveLength(1)
75+
expect(
76+
await page.textContent('text=/matrix size: 207 x 256 x 215, voxelsize: 0.74 x 0.74 x 0.74/i'),
77+
).toBeTruthy()
78+
})
79+
})

niivue/tests/Meshes.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { test, expect } from '@playwright/test'
2+
import { BASE_URL, loadTestSurfImage, loadTestSurfOverlay } from './utils'
3+
4+
test.describe('Loading meshes', () => {
5+
test('loads a test surface', async ({ page }) => {
6+
await page.goto(BASE_URL)
7+
8+
await loadTestSurfImage(page)
9+
await page.waitForTimeout(1000) // wait time to work around a bug
10+
11+
expect(await page.waitForSelector('canvas')).toBeTruthy()
12+
expect(await page.$$('canvas')).toHaveLength(1)
13+
14+
await loadTestSurfImage(page)
15+
expect(await page.$$('canvas')).toHaveLength(2)
16+
17+
expect(await page.textContent('text=/Number of Points: 40962/i')).toBeTruthy()
18+
})
19+
20+
test('loads a test surface and overlay', async ({ page }) => {
21+
await page.goto(BASE_URL)
22+
23+
await loadTestSurfImage(page)
24+
25+
await page.waitForTimeout(1000) // duplicated code to work around a bug
26+
await loadTestSurfImage(page) // duplicated code to work around a bug
27+
28+
await loadTestSurfOverlay(page, 'curv')
29+
30+
await page.waitForTimeout(1000) // duplicated code to work around a bug
31+
await loadTestSurfOverlay(page, 'curv') // duplicated code to work around a bug
32+
})
33+
})

niivue/tests/LoadingTests.spec.ts renamed to niivue/tests/WithMessage.spec.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { test, expect } from '@playwright/test'
2-
import { waitForDebugger } from 'inspector'
3-
4-
const BASE_URL = 'http://localhost:4000/'
2+
import { BASE_URL, loadTestImage } from './utils'
53

64
test.describe('app', () => {
75
test('that there are 0 canvases when no image is loaded', async ({ page }) => {
@@ -66,25 +64,10 @@ test.describe('app', () => {
6664
})
6765
})
6866

69-
async function loadTestImage(page) {
70-
const testLink = 'https://niivue.github.io/niivue-demo-images/mni152.nii.gz'
71-
// send a message to the app to load the test image
72-
const message = {
73-
type: 'addImage',
74-
body: {
75-
data: '',
76-
uri: testLink,
77-
},
78-
}
79-
await page.evaluate((m) => window.postMessage(m, '*'), message)
80-
}
81-
8267
function listenForMessage(page) {
8368
const message = page.evaluate(() => {
8469
return new Promise((resolve) => {
8570
window.addEventListener('message', (event) => {
86-
console.log(event)
87-
console.log(event.data)
8871
if (event.data?.type == 'debugAnswer') resolve(event.data.body)
8972
})
9073
})

0 commit comments

Comments
 (0)