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