Skip to content

Commit dd1242e

Browse files
Vinnlunknown
authored andcommitted
Add tests for the Scratchpad view
1 parent e3dbca5 commit dd1242e

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22
preset: 'ts-jest/presets/js-with-babel',
3-
testEnvironment: 'node',
3+
testEnvironment: 'jsdom',
44
collectCoverage: true,
55
// For some reason Jest is not measuring coverage without the below option.
66
// Unfortunately, despite `!(.test)`, it still measures coverage of test files as well:
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`View mode should properly render the pad's contents 1`] = `"<div>First line<br>Second line<br></div>"`;

scratchpad/view.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-env jest */
2+
import $rdf from 'rdflib'
3+
import vocab from 'solid-namespace'
4+
import { view } from './view'
5+
6+
const ns = vocab($rdf)
7+
8+
function addMockPad (mockStore: $rdf.IndexedFormula): $rdf.NamedNode {
9+
const mockPad = $rdf.sym('https://mock-pad')
10+
const mockFirstLine = $rdf.sym('https://arbitrary-line-1')
11+
mockStore.add(mockPad, ns.pad('next'), mockFirstLine, mockPad.doc())
12+
mockStore.add(mockFirstLine, ns.sioc('content'), 'First line', mockPad.doc())
13+
mockStore.add(mockFirstLine, ns.dc('created'), new Date(0), mockPad.doc())
14+
const mockSecondLine = $rdf.sym('https://arbitrary-line-2')
15+
mockStore.add(mockFirstLine, ns.pad('next'), mockSecondLine, mockPad.doc())
16+
mockStore.add(mockSecondLine, ns.sioc('content'), 'Second line', mockPad.doc())
17+
mockStore.add(mockSecondLine, ns.dc('created'), new Date(0), mockPad.doc())
18+
mockStore.add(mockSecondLine, ns.pad('next'), mockPad, mockPad.doc())
19+
20+
return mockPad
21+
}
22+
23+
describe('View mode', () => {
24+
it('should not show an edit button when the user is not logged in', async () => {
25+
const mockStore = $rdf.graph()
26+
const mockPad = addMockPad(mockStore)
27+
28+
const container = document.createElement('div')
29+
30+
view({
31+
container: container,
32+
subject: mockPad,
33+
store: mockStore
34+
})
35+
const button = container.querySelector('button')
36+
expect(button).toBeNull()
37+
})
38+
39+
it('should show an edit button when the user is logged in', async () => {
40+
const mockStore = $rdf.graph()
41+
const mockPad = addMockPad(mockStore)
42+
const mockUser = $rdf.sym('https://mock-user')
43+
44+
const container = document.createElement('div')
45+
46+
view({
47+
container: container,
48+
subject: mockPad,
49+
store: mockStore,
50+
user: mockUser
51+
})
52+
const button = container.querySelector('button')
53+
expect(button).toBeDefined()
54+
expect(button!.textContent).toBe('Edit')
55+
})
56+
57+
it('should properly render the pad\'s contents', async () => {
58+
const mockStore = $rdf.graph()
59+
const mockPad = addMockPad(mockStore)
60+
61+
const container = document.createElement('div')
62+
63+
view({
64+
container: container,
65+
subject: mockPad,
66+
store: mockStore
67+
})
68+
expect(container.outerHTML).toMatchSnapshot()
69+
})
70+
})

0 commit comments

Comments
 (0)