Skip to content

Commit 9a1580e

Browse files
authored
fix: remove unnecessary CI checks in tests, fix issue with history (#32)
1 parent 7fc7007 commit 9a1580e

File tree

10 files changed

+96
-49
lines changed

10 files changed

+96
-49
lines changed

integration-tests/index.ts

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ async function test() {
2626
window.assetsSnapshot = assets
2727
// we had to implement this whole history logic because there is no way
2828
// to call creator.resetCanvas(newAssets) from test code file
29-
if (currentHistoryIndex === assetsUpdatesHistory.length - 1) {
30-
assetsUpdatesHistory.push(assets)
31-
currentHistoryIndex = assetsUpdatesHistory.length - 1
29+
if (currentHistoryIndex < assetsUpdatesHistory.length - 1) {
30+
assetsUpdatesHistory.splice(currentHistoryIndex + 1)
3231
}
32+
assetsUpdatesHistory.push(assets)
33+
currentHistoryIndex = assetsUpdatesHistory.length - 1
3334
console.log(assets)
3435
},
3536
(assetId) => {
@@ -48,8 +49,10 @@ async function test() {
4849
addImageInput.value = '' // reset input value to allow re-uploading the same file
4950
})
5051

51-
const startProjectInput = document.querySelector<HTMLInputElement>('#start-project-from-images')!
52-
startProjectInput.addEventListener('change', (event) => {
52+
const startProjectInputFromImages = document.querySelector<HTMLInputElement>(
53+
'#start-project-from-images'
54+
)!
55+
startProjectInputFromImages.addEventListener('change', (event) => {
5356
const { files } = event.target as HTMLInputElement
5457
if (!files) return
5558
creator.resetAssets(
@@ -58,7 +61,28 @@ async function test() {
5861
})),
5962
true
6063
)
61-
startProjectInput.value = '' // reset input value to allow re-uploading the same file
64+
startProjectInputFromImages.value = '' // reset input value to allow re-uploading the same file
65+
})
66+
67+
const startProjectInputFroMAssets = document.querySelector<HTMLInputElement>(
68+
'#start-project-from-assets'
69+
)!
70+
startProjectInputFroMAssets.addEventListener('change', (event) => {
71+
const { files } = event.target as HTMLInputElement
72+
if (!files) return
73+
74+
const PROJECT_SAMPLE = Array.from(files).map((file) => ({
75+
url: URL.createObjectURL(file),
76+
points: [
77+
{ x: 100, y: 100, u: 0, v: 0 },
78+
{ x: 200, y: 100, u: 1, v: 0 },
79+
{ x: 200, y: 200, u: 1, v: 1 },
80+
{ x: 100, y: 200, u: 0, v: 1 },
81+
],
82+
}))
83+
84+
creator.resetAssets(PROJECT_SAMPLE, true)
85+
startProjectInputFroMAssets.value = '' // reset input value to allow re-uploading the same file
6286
})
6387

6488
removeAssetBtn.addEventListener('click', () => {

integration-tests/template.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<aside>
3333
<label>Add image<input type="file" id="add-image" /></label>
3434
<label>Start project from images<input type="file" multiple id="start-project-from-images" /></label>
35+
<label>Start project from project assets<input type="file" multiple id="start-project-from-assets" /></label>
3536
<p>Selected asset: <span id="selected-asset-id">0</span></p>
3637
<p>Is processing events: <span id="is-processing-events">false</span></p>
3738
<button id="remove-btn">Remove Asset</button>

integration-tests/tests/asset-basic-transform.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { test, expect } from '@playwright/test'
44
import init from '../init'
55

66
test('asset performs basic transformations', async ({ page }, testinfo) => {
7-
if (process.env.CI) {
8-
test.skip()
9-
return
10-
}
11-
127
testinfo.snapshotSuffix = '' // by default is `process.platform`
138

149
const utils = await init(page)

integration-tests/tests/asset-removal.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { test, expect } from '@playwright/test'
44
import init from '../init'
55

66
test('asset removal', async ({ page }, testinfo) => {
7-
if (process.env.CI) {
8-
test.skip()
9-
return
10-
}
11-
127
testinfo.snapshotSuffix = '' // by default is `process.platform`
138

149
const utils = await init(page)

integration-tests/tests/assets-snapshot.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ const STATE_AFTER_UPLOAD = [
3939
]
4040

4141
test('asset selection', async ({ page }, testinfo) => {
42-
if (process.env.CI) {
43-
test.skip()
44-
return
45-
}
4642
testinfo.snapshotSuffix = '' // by default is `process.platform`
47-
const canvas = page.locator('canvas')
4843
const assetIdEl = page.locator('#selected-asset-id')
4944

5045
const utils = await init(page)

integration-tests/tests/history.spec.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
import { test, expect } from '@playwright/test'
44
import init, { TransformHandle } from '../init'
55

6-
test('asset selection', async ({ page }, testinfo) => {
7-
if (process.env.CI) {
8-
test.skip()
9-
return
10-
}
6+
test('history', async ({ page }, testinfo) => {
117
testinfo.snapshotSuffix = '' // by default is `process.platform`
128
const undoBtn = page.locator('#undo-btn')
139
const redoBtn = page.locator('#redo-btn')
@@ -43,3 +39,29 @@ test('asset selection', async ({ page }, testinfo) => {
4339
await redoBtn.click() // redo second asset upload
4440
expect(await utils.getAssetsState()).toStrictEqual(stateSecondAssetTransform)
4541
})
42+
43+
test('history - the next update after reset_assets should be different than input of reset_assets', async ({
44+
page,
45+
}, testinfo) => {
46+
// There was an issue where we go back in history and last_snapshot was not updated(reset_asset wasn't updating it)
47+
// and with next mouse event check_assets_update function was called with same data as reset_assets got
48+
// while should not be called at all if nothing has changed
49+
50+
// this test case looks for this behaviour by checking if rolling back history was interrupted with just a mouse event(no real change in assets)
51+
52+
testinfo.snapshotSuffix = ''
53+
const undoBtn = page.locator('#undo-btn')
54+
const redoBtn = page.locator('#redo-btn')
55+
56+
const utils = await init(page)
57+
const firstAsset = await utils.uploadAsset()
58+
59+
await utils.selectAsset(firstAsset)
60+
await utils.resizeAsset(firstAsset, 200, 200, TransformHandle.BOTTOM_RIGHT)
61+
const afterResizeState = await utils.getAssetsState()
62+
await undoBtn.click()
63+
await page.mouse.move(300, 300) // move pointer on canvas
64+
await page.mouse.move(0, 0) // so now can be move out and trigger mouse leave event
65+
await redoBtn.click()
66+
expect(await utils.getAssetsState()).toStrictEqual(afterResizeState)
67+
})

integration-tests/tests/initial-assets.spec.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@ import init from '../init'
55
import { fileURLToPath } from 'url'
66
import path from 'path'
77

8+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
9+
810
test('initial assets', async ({ page }, testinfo) => {
9-
if (process.env.CI) {
10-
test.skip()
11-
return
12-
}
1311
testinfo.snapshotSuffix = '' // by default is `process.platform`
1412

1513
const utils = await init(page)
1614

17-
const __dirname = path.dirname(fileURLToPath(import.meta.url))
1815
const testImagePaths = [
1916
path.join(__dirname, '../image-sample.png'),
2017
path.join(__dirname, '../another-image-sample.jpg'),
2118
]
2219

23-
const fileInput = (await page.$('#start-project-from-images'))!
20+
const fileInput = (await page.$('#start-project-from-assets'))!
2421
await fileInput.setInputFiles(testImagePaths)
2522
const assets = await utils.getAssetsState()
2623
expect(assets.length).toBe(2)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// npm run test-e2e -- initial-images.spec.ts --debug
2+
3+
import { test, expect } from '@playwright/test'
4+
import init from '../init'
5+
import { fileURLToPath } from 'url'
6+
import path from 'path'
7+
8+
test('initial images', async ({ page }, testinfo) => {
9+
testinfo.snapshotSuffix = '' // by default is `process.platform`
10+
11+
const utils = await init(page)
12+
13+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
14+
const testImagePaths = [
15+
path.join(__dirname, '../image-sample.png'),
16+
path.join(__dirname, '../another-image-sample.jpg'),
17+
]
18+
19+
const fileInput = (await page.$('#start-project-from-images'))!
20+
await fileInput.setInputFiles(testImagePaths)
21+
const assets = await utils.getAssetsState()
22+
expect(assets.length).toBe(2)
23+
})

integration-tests/tests/ui-hover.spec.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { test, expect } from '@playwright/test'
44
import init from '../init'
55

66
test('ui elements get correct highlight on hover', async ({ page }, testinfo) => {
7-
if (process.env.CI) {
8-
test.skip()
9-
return
10-
}
11-
127
testinfo.snapshotSuffix = '' // by default is `process.platform`
138

149
const utils = await init(page)

src/logic/index.zig

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ fn generate_id() u32 {
8181
pub fn add_asset(id_or_zero: u32, points: [4]Types.PointUV, texture_id: u32) void {
8282
const id = if (id_or_zero == 0) generate_id() else id_or_zero;
8383
state.assets.put(id, Texture.new(id, points, texture_id)) catch unreachable;
84-
notify_about_assets_update();
84+
check_assets_update(true);
8585
}
8686

8787
pub fn remove_asset() void {
8888
_ = state.assets.remove(state.active_asset_id);
8989
state.active_asset_id = 0;
9090
on_asset_select_cb(state.active_asset_id);
91-
notify_about_assets_update();
91+
check_assets_update(true);
9292
}
9393

9494
pub fn on_update_pick(id: u32) void {
@@ -111,7 +111,7 @@ pub fn on_pointer_down(x: f32, y: f32) void {
111111

112112
// const std.heap.page_allocator.alloc(AssetZig, state.assets.count())
113113
var last_assets_update: []const AssetZig = &.{};
114-
fn notify_about_assets_update() void {
114+
fn check_assets_update(should_notify: bool) void {
115115
const cb = on_asset_update_cb orelse return;
116116

117117
var new_assets_update = std.heap.page_allocator.alloc(AssetZig, state.assets.count()) catch unreachable;
@@ -140,10 +140,12 @@ fn notify_about_assets_update() void {
140140
std.heap.page_allocator.free(last_assets_update);
141141
last_assets_update = new_assets_update;
142142

143-
if (new_assets_update.len > 0) {
144-
cb(new_assets_update); // would throw error if results.len == 0
145-
} else {
146-
cb(&.{});
143+
if (should_notify) {
144+
if (new_assets_update.len > 0) {
145+
cb(new_assets_update); // would throw error if results.len == 0
146+
} else {
147+
cb(&.{});
148+
}
147149
}
148150
}
149151

@@ -153,7 +155,7 @@ pub fn on_pointer_up() void {
153155
on_asset_select_cb(state.active_asset_id);
154156
} else {
155157
state.ongoing_action = .none;
156-
notify_about_assets_update();
158+
check_assets_update(true);
157159
}
158160
}
159161

@@ -192,7 +194,7 @@ pub fn on_pointer_move(x: f32, y: f32) void {
192194
pub fn on_pointer_leave() void {
193195
state.ongoing_action = .none;
194196
state.hovered_asset_id = 0;
195-
notify_about_assets_update();
197+
check_assets_update(true);
196198
}
197199

198200
fn get_border() struct { []f32, []f32 } { // { triangle vertex, msdf vertex }
@@ -332,9 +334,7 @@ pub fn reset_assets(new_assets: []const AssetZig, with_snapshot: bool) void {
332334

333335
on_asset_update_cb = real_callback_pointer;
334336

335-
if (with_snapshot) {
336-
notify_about_assets_update();
337-
}
337+
check_assets_update(with_snapshot);
338338
}
339339

340340
pub fn destroy_state() void {

0 commit comments

Comments
 (0)