-
Notifications
You must be signed in to change notification settings - Fork 85
feat(runtime): add preview.pathname
#233
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,78 @@ | ||
import { assert, expect, test } from 'vitest'; | ||
import { PreviewsStore } from './previews.js'; | ||
import type { PortListener, WebContainer } from '@webcontainer/api'; | ||
|
||
test("preview is set ready on webcontainer's event", async () => { | ||
const { store, emit } = await getStore(); | ||
store.setPreviews([3000]); | ||
|
||
assert(store.previews.value); | ||
expect(store.previews.value[0].ready).toBe(false); | ||
|
||
emit(3000, 'open', 'https://localhost'); | ||
|
||
expect(store.previews.value![0].ready).toBe(true); | ||
}); | ||
|
||
test('preview is not set ready when different port is ready', async () => { | ||
const { store, emit } = await getStore(); | ||
store.setPreviews([3000]); | ||
|
||
assert(store.previews.value); | ||
expect(store.previews.value[0].ready).toBe(false); | ||
|
||
emit(3001, 'open', 'https://localhost'); | ||
|
||
expect(store.previews.value[0].ready).toBe(false); | ||
}); | ||
|
||
test('marks multiple preview infos ready', async () => { | ||
const { store, emit } = await getStore(); | ||
store.setPreviews([ | ||
{ port: 3000, title: 'Dev' }, | ||
{ port: 3000, title: 'Docs', pathname: '/docs' }, | ||
]); | ||
|
||
assert(store.previews.value); | ||
expect(store.previews.value).toHaveLength(2); | ||
|
||
expect(store.previews.value[0].ready).toBe(false); | ||
expect(store.previews.value[0].pathname).toBe(undefined); | ||
|
||
expect(store.previews.value[1].ready).toBe(false); | ||
expect(store.previews.value[1].pathname).toBe('/docs'); | ||
|
||
emit(3000, 'open', 'https://localhost'); | ||
|
||
expect(store.previews.value[0].ready).toBe(true); | ||
expect(store.previews.value[1].ready).toBe(true); | ||
}); | ||
|
||
async function getStore() { | ||
const listeners: PortListener[] = []; | ||
|
||
const webcontainer: Pick<WebContainer, 'on'> = { | ||
on: (type, listener) => { | ||
if (type === 'port') { | ||
listeners.push(listener as PortListener); | ||
} | ||
|
||
return () => undefined; | ||
}, | ||
}; | ||
|
||
const promise = new Promise<WebContainer>((resolve) => { | ||
resolve(webcontainer as WebContainer); | ||
}); | ||
|
||
await promise; | ||
|
||
return { | ||
store: new PreviewsStore(promise), | ||
emit: (...args: Parameters<PortListener>) => { | ||
assert(listeners.length > 0, 'Port listeners were not captured'); | ||
|
||
listeners.forEach((cb) => cb(...args)); | ||
}, | ||
}; | ||
} |
This file contains hidden or 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 hidden or 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,7 @@ | ||
export class PortInfo { | ||
constructor( | ||
readonly port: number, | ||
public origin?: string, | ||
public ready: boolean = false, | ||
) {} | ||
} |
This file contains hidden or 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,80 +1,120 @@ | ||
import { describe, it, expect } from 'vitest'; | ||
import { PreviewInfo } from './preview-info.js'; | ||
import { PortInfo } from './port-info.js'; | ||
|
||
describe('PreviewInfo', () => { | ||
it('should accept a port', () => { | ||
const previewInfo = new PreviewInfo(3000); | ||
it('should accept a number for port', () => { | ||
const previewInfo = PreviewInfo.parse(3000); | ||
|
||
expect(previewInfo.port).toBe(3000); | ||
expect(previewInfo.title).toBe(undefined); | ||
expect(previewInfo.pathname).toBe(undefined); | ||
}); | ||
|
||
it('should accept a string for port and pathname', () => { | ||
const previewInfo = PreviewInfo.parse('3000/some/nested/path'); | ||
|
||
expect(previewInfo.port).toBe(3000); | ||
expect(previewInfo.pathname).toBe('some/nested/path'); | ||
expect(previewInfo.title).toBe(undefined); | ||
}); | ||
|
||
it('should accept a tuple of [port, title]', () => { | ||
const previewInfo = new PreviewInfo([3000, 'Local server']); | ||
const previewInfo = PreviewInfo.parse([3000, 'Local server']); | ||
|
||
expect(previewInfo.port).toBe(3000); | ||
expect(previewInfo.title).toBe('Local server'); | ||
expect(previewInfo.pathname).toBe(undefined); | ||
}); | ||
|
||
it('should accept a tuple of [port, title, pathname]', () => { | ||
const previewInfo = PreviewInfo.parse([3000, 'Local server', '/docs']); | ||
|
||
expect(previewInfo.port).toBe(3000); | ||
expect(previewInfo.title).toBe('Local server'); | ||
expect(previewInfo.pathname).toBe('/docs'); | ||
}); | ||
|
||
it('should accept an object with { port, title }', () => { | ||
const previewInfo = new PreviewInfo({ port: 3000, title: 'Local server' }); | ||
const previewInfo = PreviewInfo.parse({ port: 3000, title: 'Local server' }); | ||
|
||
expect(previewInfo.port).toBe(3000); | ||
expect(previewInfo.title).toBe('Local server'); | ||
expect(previewInfo.pathname).toBe(undefined); | ||
}); | ||
|
||
it('should accept an object with { port, title, pathname }', () => { | ||
const previewInfo = PreviewInfo.parse({ port: 3000, title: 'Local server', pathname: '/docs' }); | ||
|
||
expect(previewInfo.port).toBe(3000); | ||
expect(previewInfo.title).toBe('Local server'); | ||
expect(previewInfo.pathname).toBe('/docs'); | ||
}); | ||
|
||
it('should not be ready by default', () => { | ||
const previewInfo = new PreviewInfo(3000); | ||
const previewInfo = new PreviewInfo({}, new PortInfo(3000)); | ||
|
||
expect(previewInfo.ready).toBe(false); | ||
}); | ||
|
||
it('should be ready if explicitly set', () => { | ||
const previewInfo = new PreviewInfo(3000, true); | ||
const previewInfo = new PreviewInfo({}, new PortInfo(3000, undefined, true)); | ||
|
||
expect(previewInfo.ready).toBe(true); | ||
}); | ||
|
||
it('should not be ready if explicitly set', () => { | ||
const previewInfo = new PreviewInfo(3000, false); | ||
const previewInfo = new PreviewInfo({}, new PortInfo(3000, undefined, false)); | ||
|
||
expect(previewInfo.ready).toBe(false); | ||
}); | ||
|
||
it('should have a url with a custom pathname and baseUrl', () => { | ||
const previewInfo = new PreviewInfo(3000); | ||
previewInfo.baseUrl = 'https://example.com'; | ||
previewInfo.pathname = '/foo'; | ||
const parsed = PreviewInfo.parse('3000/foo'); | ||
const previewInfo = new PreviewInfo(parsed, new PortInfo(parsed.port)); | ||
previewInfo.portInfo.origin = 'https://example.com'; | ||
|
||
expect(previewInfo.url).toBe('https://example.com/foo'); | ||
}); | ||
|
||
it('should be equal to another preview info with the same port and title', () => { | ||
const a = new PreviewInfo(3000); | ||
const b = new PreviewInfo(3000); | ||
const a = new PreviewInfo({}, new PortInfo(3000)); | ||
const b = new PreviewInfo({}, new PortInfo(3000)); | ||
|
||
expect(PreviewInfo.equals(a, b)).toBe(true); | ||
}); | ||
|
||
it('should not be equal to another preview info with a different port', () => { | ||
const a = new PreviewInfo(3000); | ||
const b = new PreviewInfo(4000); | ||
const a = new PreviewInfo({}, new PortInfo(3000)); | ||
const b = new PreviewInfo({}, new PortInfo(4000)); | ||
|
||
expect(PreviewInfo.equals(a, b)).toBe(false); | ||
}); | ||
|
||
it('should not be equal to another preview info with a different title', () => { | ||
const a = new PreviewInfo([3000, 'Local server']); | ||
const b = new PreviewInfo([3000, 'Remote server']); | ||
const parsed = { | ||
a: PreviewInfo.parse([3000, 'Local server']), | ||
b: PreviewInfo.parse([3000, 'Remote server']), | ||
}; | ||
|
||
const a = new PreviewInfo(parsed.a, new PortInfo(parsed.a.port)); | ||
const b = new PreviewInfo(parsed.b, new PortInfo(parsed.b.port)); | ||
|
||
expect(PreviewInfo.equals(a, b)).toBe(false); | ||
}); | ||
|
||
it('should not be equal to another preview info with a different pathname', () => { | ||
const a = new PreviewInfo(3000); | ||
const b = new PreviewInfo(3000); | ||
const parsed = { | ||
a: PreviewInfo.parse(3000), | ||
b: PreviewInfo.parse('3000/b'), | ||
c: PreviewInfo.parse('3000/c'), | ||
}; | ||
|
||
a.pathname = '/foo'; | ||
const a = new PreviewInfo(parsed.a, new PortInfo(parsed.a.port)); | ||
const b = new PreviewInfo(parsed.b, new PortInfo(parsed.b.port)); | ||
const c = new PreviewInfo(parsed.c, new PortInfo(parsed.c.port)); | ||
|
||
expect(PreviewInfo.equals(a, b)).toBe(false); | ||
expect(PreviewInfo.equals(b, c)).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.