|
1 | 1 | import { describe, it, expect } from 'vitest';
|
2 | 2 | import { PreviewInfo } from './preview-info.js';
|
| 3 | +import { PortInfo } from './port-info.js'; |
3 | 4 |
|
4 | 5 | describe('PreviewInfo', () => {
|
5 |
| - it('should accept a port', () => { |
6 |
| - const previewInfo = new PreviewInfo(3000); |
| 6 | + it('should accept a number for port', () => { |
| 7 | + const previewInfo = PreviewInfo.parse(3000); |
7 | 8 |
|
8 | 9 | expect(previewInfo.port).toBe(3000);
|
| 10 | + expect(previewInfo.title).toBe(undefined); |
| 11 | + expect(previewInfo.pathname).toBe(undefined); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should accept a string for port and pathname', () => { |
| 15 | + const previewInfo = PreviewInfo.parse('3000/some/nested/path'); |
| 16 | + |
| 17 | + expect(previewInfo.port).toBe(3000); |
| 18 | + expect(previewInfo.pathname).toBe('some/nested/path'); |
| 19 | + expect(previewInfo.title).toBe(undefined); |
9 | 20 | });
|
10 | 21 |
|
11 | 22 | it('should accept a tuple of [port, title]', () => {
|
12 |
| - const previewInfo = new PreviewInfo([3000, 'Local server']); |
| 23 | + const previewInfo = PreviewInfo.parse([3000, 'Local server']); |
13 | 24 |
|
14 | 25 | expect(previewInfo.port).toBe(3000);
|
15 | 26 | expect(previewInfo.title).toBe('Local server');
|
| 27 | + expect(previewInfo.pathname).toBe(undefined); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should accept a tuple of [port, title, pathname]', () => { |
| 31 | + const previewInfo = PreviewInfo.parse([3000, 'Local server', '/docs']); |
| 32 | + |
| 33 | + expect(previewInfo.port).toBe(3000); |
| 34 | + expect(previewInfo.title).toBe('Local server'); |
| 35 | + expect(previewInfo.pathname).toBe('/docs'); |
16 | 36 | });
|
17 | 37 |
|
18 | 38 | it('should accept an object with { port, title }', () => {
|
19 |
| - const previewInfo = new PreviewInfo({ port: 3000, title: 'Local server' }); |
| 39 | + const previewInfo = PreviewInfo.parse({ port: 3000, title: 'Local server' }); |
20 | 40 |
|
21 | 41 | expect(previewInfo.port).toBe(3000);
|
22 | 42 | expect(previewInfo.title).toBe('Local server');
|
| 43 | + expect(previewInfo.pathname).toBe(undefined); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should accept an object with { port, title, pathname }', () => { |
| 47 | + const previewInfo = PreviewInfo.parse({ port: 3000, title: 'Local server', pathname: '/docs' }); |
| 48 | + |
| 49 | + expect(previewInfo.port).toBe(3000); |
| 50 | + expect(previewInfo.title).toBe('Local server'); |
| 51 | + expect(previewInfo.pathname).toBe('/docs'); |
23 | 52 | });
|
24 | 53 |
|
25 | 54 | it('should not be ready by default', () => {
|
26 |
| - const previewInfo = new PreviewInfo(3000); |
| 55 | + const previewInfo = new PreviewInfo({}, new PortInfo(3000)); |
27 | 56 |
|
28 | 57 | expect(previewInfo.ready).toBe(false);
|
29 | 58 | });
|
30 | 59 |
|
31 | 60 | it('should be ready if explicitly set', () => {
|
32 |
| - const previewInfo = new PreviewInfo(3000, true); |
| 61 | + const previewInfo = new PreviewInfo({}, new PortInfo(3000, undefined, true)); |
33 | 62 |
|
34 | 63 | expect(previewInfo.ready).toBe(true);
|
35 | 64 | });
|
36 | 65 |
|
37 | 66 | it('should not be ready if explicitly set', () => {
|
38 |
| - const previewInfo = new PreviewInfo(3000, false); |
| 67 | + const previewInfo = new PreviewInfo({}, new PortInfo(3000, undefined, false)); |
39 | 68 |
|
40 | 69 | expect(previewInfo.ready).toBe(false);
|
41 | 70 | });
|
42 | 71 |
|
43 | 72 | it('should have a url with a custom pathname and baseUrl', () => {
|
44 |
| - const previewInfo = new PreviewInfo(3000); |
45 |
| - previewInfo.baseUrl = 'https://example.com'; |
46 |
| - previewInfo.pathname = '/foo'; |
| 73 | + const parsed = PreviewInfo.parse('3000/foo'); |
| 74 | + const previewInfo = new PreviewInfo(parsed, new PortInfo(parsed.port)); |
| 75 | + previewInfo.portInfo.origin = 'https://example.com'; |
47 | 76 |
|
48 | 77 | expect(previewInfo.url).toBe('https://example.com/foo');
|
49 | 78 | });
|
50 | 79 |
|
51 | 80 | it('should be equal to another preview info with the same port and title', () => {
|
52 |
| - const a = new PreviewInfo(3000); |
53 |
| - const b = new PreviewInfo(3000); |
| 81 | + const a = new PreviewInfo({}, new PortInfo(3000)); |
| 82 | + const b = new PreviewInfo({}, new PortInfo(3000)); |
54 | 83 |
|
55 | 84 | expect(PreviewInfo.equals(a, b)).toBe(true);
|
56 | 85 | });
|
57 | 86 |
|
58 | 87 | it('should not be equal to another preview info with a different port', () => {
|
59 |
| - const a = new PreviewInfo(3000); |
60 |
| - const b = new PreviewInfo(4000); |
| 88 | + const a = new PreviewInfo({}, new PortInfo(3000)); |
| 89 | + const b = new PreviewInfo({}, new PortInfo(4000)); |
61 | 90 |
|
62 | 91 | expect(PreviewInfo.equals(a, b)).toBe(false);
|
63 | 92 | });
|
64 | 93 |
|
65 | 94 | it('should not be equal to another preview info with a different title', () => {
|
66 |
| - const a = new PreviewInfo([3000, 'Local server']); |
67 |
| - const b = new PreviewInfo([3000, 'Remote server']); |
| 95 | + const parsed = { |
| 96 | + a: PreviewInfo.parse([3000, 'Local server']), |
| 97 | + b: PreviewInfo.parse([3000, 'Remote server']), |
| 98 | + }; |
| 99 | + |
| 100 | + const a = new PreviewInfo(parsed.a, new PortInfo(parsed.a.port)); |
| 101 | + const b = new PreviewInfo(parsed.b, new PortInfo(parsed.b.port)); |
68 | 102 |
|
69 | 103 | expect(PreviewInfo.equals(a, b)).toBe(false);
|
70 | 104 | });
|
71 | 105 |
|
72 | 106 | it('should not be equal to another preview info with a different pathname', () => {
|
73 |
| - const a = new PreviewInfo(3000); |
74 |
| - const b = new PreviewInfo(3000); |
| 107 | + const parsed = { |
| 108 | + a: PreviewInfo.parse(3000), |
| 109 | + b: PreviewInfo.parse('3000/b'), |
| 110 | + c: PreviewInfo.parse('3000/c'), |
| 111 | + }; |
75 | 112 |
|
76 |
| - a.pathname = '/foo'; |
| 113 | + const a = new PreviewInfo(parsed.a, new PortInfo(parsed.a.port)); |
| 114 | + const b = new PreviewInfo(parsed.b, new PortInfo(parsed.b.port)); |
| 115 | + const c = new PreviewInfo(parsed.c, new PortInfo(parsed.c.port)); |
77 | 116 |
|
78 | 117 | expect(PreviewInfo.equals(a, b)).toBe(false);
|
| 118 | + expect(PreviewInfo.equals(b, c)).toBe(false); |
79 | 119 | });
|
80 | 120 | });
|
0 commit comments