Skip to content

Commit cff201c

Browse files
committed
Add tests for v1
1 parent 9a1be5a commit cff201c

File tree

3 files changed

+95
-174
lines changed

3 files changed

+95
-174
lines changed

playwright.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/** @type {import('@playwright/test').PlaywrightTestConfig} */
22
const config = {
33
webServer: {
4-
command: 'npm run build && npm run preview',
5-
port: 4173
4+
command: 'npm run dev',
5+
port: 5173,
6+
reuseExistingServer: true
67
},
78
testDir: 'tests'
89
}

src/routes/+page.svelte

Lines changed: 68 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<script>
2-
import JsonView from '$lib'
2+
import { JsonView } from '$lib'
33
44
/* eslint-disable */
5-
const test1 = [
5+
const fixture = [
66
{
7+
desc: 'Test basic',
78
_id: '60702426463e99454c8b9ce4',
89
index: 0,
910
guid: 'c47f144e-9390-4e96-8b9f-7f3d0fb4250a',
@@ -30,154 +31,41 @@ const test1 = [
3031
{ id: 2, name: 'Mcclure Tate', address: null }
3132
],
3233
greeting: 'Hello, Hampton Hardin! You have 2 unread messages.',
33-
favoriteFruit: 'banana',
34-
contacts: [],
35-
report: {},
36-
email2: null,
37-
email3: '',
38-
email4: undefined
34+
favoriteFruit: 'banana'
3935
},
4036
{
41-
_id: '60702426f2aec6774924fd63',
42-
index: 1,
43-
guid: '027c7f09-4161-47b2-91c5-01f675094e40',
44-
isActive: true,
45-
balance: '$2,875.12',
46-
picture: 'http://placehold.it/32x32',
47-
age: 28,
48-
eyeColor: 'brown',
49-
name: 'Atkinson Giles',
50-
gender: 'male',
51-
company: 'BESTO',
52-
53-
phone: '+1 (986) 474-3672',
54-
address: '906 Classon Avenue, Riverton, Guam, 4080',
55-
about:
56-
'Consectetur esse et exercitation irure qui adipisicing magna laboris cupidatat incididunt fugiat est. Occaecat irure magna do non cillum magna veniam occaecat. Ullamco officia irure id voluptate consequat amet aliquip in eiusmod deserunt aliquip exercitation culpa excepteur. Enim occaecat consectetur ex non ipsum fugiat amet et proident esse. Commodo cillum ad eiusmod enim sunt sint officia commodo et laborum.\r\n',
57-
registered: '2019-05-23T01:23:32 -08:00',
58-
latitude: -37.633187,
59-
longitude: -126.992778,
60-
tags: ['esse', 'nostrud', 'et', 'ex', 'deserunt', 'veniam', 'veniam'],
61-
friends: [
62-
{ id: 0, name: 'Millicent Mcconnell' },
63-
{ id: 1, name: 'Jeanne Wise' },
64-
{ id: 2, name: 'Nichols Roberts' }
65-
],
66-
greeting: 'Hello, Atkinson Giles! You have 1 unread messages.',
67-
favoriteFruit: 'apple'
68-
},
69-
{
70-
_id: '607024260a4713fae5019419',
71-
index: 2,
72-
guid: 'b32f9f9b-0def-46a1-a286-a8196ec4638c',
73-
isActive: false,
74-
balance: '$3,489.63',
75-
picture: 'http://placehold.it/32x32',
76-
age: 36,
77-
eyeColor: 'blue',
78-
name: 'Desiree William',
79-
gender: 'female',
80-
company: 'LYRICHORD',
81-
82-
phone: '+1 (975) 485-3348',
83-
address: '993 Thatford Avenue, Wells, Missouri, 2351',
84-
about:
85-
'Eu nostrud sunt occaecat adipisicing consectetur ipsum aute non voluptate pariatur aliquip excepteur. Minim ea aliqua do dolore tempor aliquip irure. Amet amet quis incididunt irure aute. Lorem amet reprehenderit adipisicing do laborum incididunt labore incididunt laboris velit consequat ut aute. Ea exercitation ut non dolore culpa dolore officia et elit do. Ullamco deserunt cillum aliqua tempor ipsum officia. Lorem cupidatat cillum velit est minim aliquip qui ipsum.\r\n',
86-
registered: '2015-08-04T04:33:40 -08:00',
87-
latitude: -15.687988,
88-
longitude: -144.842709,
89-
tags: ['do', 'laborum', 'fugiat', 'qui', 'duis', 'eu', 'eiusmod'],
90-
friends: [
91-
{ id: 0, name: 'Baxter Fulton' },
92-
{ id: 1, name: 'Buckley Dean' },
93-
{ id: 2, name: 'Carmen Fitzpatrick' }
94-
],
95-
greeting: 'Hello, Desiree William! You have 8 unread messages.',
96-
favoriteFruit: 'apple'
37+
desc: 'Test nested',
38+
nest0: {
39+
nest1: {
40+
nest2: {
41+
nest3: {
42+
nest4: {
43+
nest5: {
44+
nest6: {
45+
nest7: {
46+
foo: 'bar'
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
53+
}
54+
},
55+
arr: [[[[[[['baz']]]]]]]
9756
},
9857
{
99-
_id: '60702426dce125ecccee577f',
100-
index: 3,
101-
guid: '0e1d5f0d-84cd-49fe-9b69-1540ab57d509',
102-
isActive: true,
103-
balance: '$3,254.06',
104-
picture: 'http://placehold.it/32x32',
105-
age: 20,
106-
eyeColor: 'blue',
107-
name: 'John Aguirre',
108-
gender: 'female',
109-
company: 'GINK',
110-
111-
phone: '+1 (828) 407-2949',
112-
address: '958 Caton Avenue, Waterford, Puerto Rico, 6386',
113-
about:
114-
'Consequat labore minim excepteur ullamco ex consequat. Occaecat dolore magna enim eiusmod qui dolore sint qui officia. Laborum consectetur fugiat officia et sit incididunt veniam dolor culpa. Sunt anim non id sit ut ipsum laborum est sint commodo nulla incididunt incididunt. Adipisicing eiusmod voluptate deserunt minim esse reprehenderit. Irure elit culpa quis non fugiat aute proident dolore elit consectetur nostrud eu consequat nisi. Ex adipisicing in nisi proident officia.\r\n',
115-
registered: '2014-09-06T10:10:11 -08:00',
116-
latitude: 41.628236,
117-
longitude: 161.223523,
118-
tags: ['laboris', 'exercitation', 'eiusmod', 'quis', 'ad', 'amet', 'adipisicing'],
119-
friends: [
120-
{ id: 0, name: 'Madden Ballard' },
121-
{ id: 1, name: 'Ferguson Gibbs' },
122-
{ id: 2, name: 'Allyson Pickett' }
123-
],
124-
greeting: 'Hello, John Aguirre! You have 7 unread messages.',
125-
favoriteFruit: 'apple'
126-
},
127-
{
128-
_id: '6070242649fb4bc3dfca54c2',
129-
index: 4,
130-
guid: '0f4b2fef-f38f-4c93-925c-01f4d015a04a',
131-
isActive: false,
132-
balance: '$2,684.08',
133-
picture: 'http://placehold.it/32x32',
134-
age: 25,
135-
eyeColor: 'brown',
136-
name: 'Clark Rollins',
137-
gender: 'male',
138-
company: 'KIDGREASE',
139-
140-
phone: '+1 (945) 464-2097',
141-
address: '515 Hancock Street, Columbus, Florida, 6747',
142-
about:
143-
'Lorem nostrud quis amet qui. Nisi cupidatat ullamco cillum enim culpa labore fugiat ea laboris in ipsum. Proident enim dolore duis ut deserunt eiusmod consectetur minim ipsum aliquip est commodo.\r\n',
144-
registered: '2017-09-05T12:57:25 -08:00',
145-
latitude: -4.239975,
146-
longitude: 179.856364,
147-
tags: ['mollit', 'fugiat', 'dolor', 'occaecat', 'dolor', 'aliquip', 'ullamco'],
148-
friends: [
149-
{ id: 0, name: 'Humphrey Simmons' },
150-
{ id: 1, name: 'Lela Berg' },
151-
{ id: 2, name: 'Branch Holder' }
152-
],
153-
greeting: 'Hello, Clark Rollins! You have 3 unread messages.',
154-
favoriteFruit: 'apple'
58+
desc: 'Test special types',
59+
fn: function () {},
60+
sym: Symbol('bar'),
61+
null: null,
62+
undefined: undefined,
63+
empty_string: '',
64+
empty_obj: {},
65+
empty_arr: []
15566
}
15667
]
157-
const test2 = {
158-
random: 85,
159-
'random float': 75.744,
160-
bool: true,
161-
date: '1996-12-26',
162-
regEx: 'helloooooooooooooooo to you',
163-
enum: 'generator',
164-
firstname: 'Evaleen',
165-
lastname: 'Dorothy',
166-
city: 'Labasa',
167-
country: 'Virgin Islands, U.S.',
168-
countryCode: 'TL',
169-
'email uses current data': '[email protected]',
170-
'email from expression': '[email protected]',
171-
array: ['Glynnis', 'Antonietta', 'Penelopa', 'Briney', 'Viki'],
172-
'array of objects': [
173-
{ index: 0, 'index start at 5': 5 },
174-
{ index: 1, 'index start at 5': 6 },
175-
{ index: 2, 'index start at 5': 7 }
176-
],
177-
Bobbi: { age: 31 }
178-
}
179-
const test3 = 'test invalid json'
180-
const test4 = { desc: 'Test special types', foo: function () {}, bar: Symbol('bar'), null: null }
68+
const fixture2 = 'test invalid object'
18169
/* eslint-enable */
18270
18371
let depth = 0
@@ -187,37 +75,47 @@ let depth = 0
18775
<title>svelte-json-view</title>
18876
</svelte:head>
18977

190-
<button
191-
on:click={() => {
192-
depth = Infinity
193-
}}>EXPAND ALL</button
194-
>
195-
<button
196-
on:click={() => {
197-
depth = 0
198-
}}>COLLAPSE ALL</button
199-
>
200-
<br /><br />
201-
<div class="wrap">
202-
<JsonView json={test1} {depth} />
203-
</div>
204-
<hr />
205-
<div class="wrap">
206-
<JsonView json={test2} depth="0" />
207-
</div>
208-
<hr />
209-
<div class="wrap">
210-
<JsonView json={test3} />
211-
</div>
212-
<hr />
213-
<div class="wrap">
214-
<JsonView json={test4} />
215-
</div>
78+
<section>
79+
<h1>Basic</h1>
80+
<button on:click={() => (depth = 0)} id="btn_0">DEPTH 0</button>
81+
<button on:click={() => (depth = 1)} id="btn_1">DEPTH 1</button>
82+
<button on:click={() => (depth = Infinity)} id="btn_i">DEPTH INFINITY</button>
83+
<br /><br />
84+
<div class="wrap" id="basic">
85+
<JsonView json={fixture} {depth} />
86+
</div>
87+
</section>
88+
89+
<section>
90+
<h1>Test default depth=1</h1>
91+
<div class="wrap" id="depth_1">
92+
<JsonView json={fixture[0]} depth={1} />
93+
</div>
94+
</section>
95+
96+
<section>
97+
<h1>Test special types</h1>
98+
<div class="wrap" id="special_types">
99+
<JsonView json={fixture[2]} />
100+
</div>
101+
</section>
102+
103+
<section>
104+
<h1>Test invalid object</h1>
105+
<div class="wrap" id="invalid_obj">
106+
<JsonView json={fixture2} />
107+
</div>
108+
</section>
216109

217110
<style>
111+
section {
112+
margin: 0 0 4rem 0;
113+
border-bottom: 1px solid black;
114+
}
218115
.wrap {
219116
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, 'Liberation Mono',
220117
'Courier New', monospace;
221118
font-size: 0.875rem;
119+
color: #374151;
222120
}
223121
</style>

tests/test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
import { expect, test } from '@playwright/test'
22

3-
test('index page has expected h1', async ({ page }) => {
3+
test('depth=0', async ({ page }) => {
44
await page.goto('/')
5-
expect(await page.textContent('h1')).toBe('Welcome to SvelteKit')
5+
const list = await page.locator('#basic ._jsonList ._jsonBkt').allTextContents()
6+
for (const i of list) expect(i).toBe('{...}')
7+
})
8+
9+
test('depth=infinity', async ({ page }) => {
10+
await page.goto('/')
11+
await page.locator('#btn_i').click()
12+
await expect(page.locator('#basic').getByText('{...}', { exact: true })).toHaveCount(0)
13+
})
14+
15+
test('default depth=1', async ({ page }) => {
16+
await page.goto('/')
17+
const list = await page.locator('#depth_1').getByText('{...}').allTextContents()
18+
expect(list.length).toBe(3)
19+
})
20+
21+
test('special types', async ({ page }) => {
22+
await page.goto('/')
23+
const list = await page.locator('#special_types li ._jsonVal').allTextContents()
24+
const asserts = ['"Test special types"', 'f () {...}', 'Symbol(bar)', 'null', 'undefined', '""']
25+
for (let a = 0; a < list.length; a++) {
26+
expect(list[a]).toStrictEqual(asserts[a])
27+
}
628
})

0 commit comments

Comments
 (0)