Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate file-delete-restore playground test to hmr playground #11176

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions playground/file-delete-restore/App.jsx

This file was deleted.

3 changes: 0 additions & 3 deletions playground/file-delete-restore/Child.jsx

This file was deleted.

This file was deleted.

10 changes: 0 additions & 10 deletions playground/file-delete-restore/index.html

This file was deleted.

18 changes: 0 additions & 18 deletions playground/file-delete-restore/package.json

This file was deleted.

12 changes: 0 additions & 12 deletions playground/file-delete-restore/vite.config.js

This file was deleted.

61 changes: 61 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { beforeAll, describe, expect, it, test } from 'vitest'
import {
addFile,
browserLogs,
editFile,
getBg,
isBuild,
page,
removeFile,
untilBrowserLogAfter,
untilUpdated,
viteTestUrl,
Expand Down Expand Up @@ -713,4 +715,63 @@ if (!isBuild) {
await navigationPromise
}, /500/)
})

test('should hmr when file is deleted and restored', async () => {
await page.goto(viteTestUrl)

const parentFile = 'file-delete-restore/parent.js'
const childFile = 'file-delete-restore/child.js'

await untilUpdated(
() => page.textContent('.file-delete-restore'),
'parent:child',
)

editFile(childFile, (code) =>
code.replace("value = 'child'", "value = 'child1'"),
)
await untilUpdated(
() => page.textContent('.file-delete-restore'),
'parent:child1',
)

editFile(parentFile, (code) =>
code.replace(
"export { value as childValue } from './child'",
"export const childValue = 'not-child'",
),
)
removeFile(childFile)
await untilUpdated(
() => page.textContent('.file-delete-restore'),
'parent:not-child',
)

addFile(
childFile,
`
import { rerender } from './runtime'

export const value = 'child'

if (import.meta.hot) {
import.meta.hot.accept((newMod) => {
if (!newMod) return

rerender({ child: newMod.value })
})
}
`,
)
editFile(parentFile, (code) =>
code.replace(
"export const childValue = 'not-child'",
"export { value as childValue } from './child'",
),
)
await untilUpdated(
() => page.textContent('.file-delete-restore'),
'parent:child',
)
})
}
11 changes: 11 additions & 0 deletions playground/hmr/file-delete-restore/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { rerender } from './runtime'

export const value = 'child'

if (import.meta.hot) {
import.meta.hot.accept((newMod) => {
if (!newMod) return

rerender({ child: newMod.value })
})
}
4 changes: 4 additions & 0 deletions playground/hmr/file-delete-restore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { render } from './runtime'
import { childValue, parentValue } from './parent'

render({ parent: parentValue, child: childValue })
12 changes: 12 additions & 0 deletions playground/hmr/file-delete-restore/parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { rerender } from './runtime'

export const parentValue = 'parent'
export { value as childValue } from './child'

if (import.meta.hot) {
import.meta.hot.accept((newMod) => {
if (!newMod) return

rerender({ child: newMod.childValue, parent: newMod.parentValue })
})
}
16 changes: 16 additions & 0 deletions playground/hmr/file-delete-restore/runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
let state = {}

export const render = (newState) => {
state = newState
apply()
}

export const rerender = (updates) => {
state = { ...state, ...updates }
apply()
}

const apply = () => {
document.querySelector('.file-delete-restore').textContent =
Object.values(state).join(':')
}
1 change: 1 addition & 0 deletions playground/hmr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { virtual } from 'virtual:file'
import { foo as depFoo, nestedFoo } from './hmrDep'
import './importing-updated'
import './invalidation/parent'
import './file-delete-restore'

export const foo = 1
text('.app', foo)
Expand Down
1 change: 1 addition & 0 deletions playground/hmr/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
<div class="link-tag-removed">no</div>
<div class="import-image"></div>
<div class="importing-reloaded"></div>
<div class="file-delete-restore"></div>
80 changes: 3 additions & 77 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.