Skip to content

Commit 71cdbac

Browse files
committed
Improve asynchronous behavior of page tests
The changes in this commit focus on improving the asynchronous behavior of the page tests. The main changes are: - Refactor the tests to use `async/await` syntax for better readability and error handling. - Add `return new Promise` blocks to ensure the tests wait for callbacks to complete.
1 parent 9beea9e commit 71cdbac

File tree

2 files changed

+57
-46
lines changed

2 files changed

+57
-46
lines changed

test/page.js

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,61 +19,72 @@ console.log('testid', testid)
1919

2020
describe('page', () => {
2121
describe('#page.put()', () => {
22-
it('should save a page', () => {
23-
page.put('asdf', testpage, e => {
24-
if (e) throw e
22+
it('should save a page', async () => {
23+
return new Promise(resolve => {
24+
page.put('asdf', testpage, e => {
25+
if (e) throw e
26+
resolve()
27+
})
2528
})
2629
})
2730
})
2831
describe('#page.get()', () => {
29-
it('should get a page if it exists', () => {
30-
page.get('asdf', (e, got) => {
31-
if (e) throw e
32-
assert.equal(got.title, 'Asdf')
32+
it('should get a page if it exists', async () => {
33+
return new Promise(resolve => {
34+
page.get('asdf', (e, got) => {
35+
if (e) throw e
36+
assert.equal(got.title, 'Asdf')
37+
resolve()
38+
})
3339
})
3440
})
35-
it('should copy a page from default if nonexistant in db', () => {
36-
page.get('welcome-visitors', (e, got) => {
37-
if (e) throw e
38-
assert.equal(got.title, 'Welcome Visitors')
41+
it('should copy a page from default if nonexistant in db', async () => {
42+
return new Promise(resolve => {
43+
page.get('welcome-visitors', (e, got) => {
44+
if (e) throw e
45+
assert.equal(got.title, 'Welcome Visitors')
46+
resolve()
47+
})
3948
})
4049
})
4150
// note: here we assume the wiki-plugin-activity repo has been cloned into an adjacent directory
42-
it('should copy a page from plugins if nonexistant in db', () => {
43-
page.get('recent-changes', (e, got) => {
44-
if (e) throw e
45-
assert.equal(got.title, 'Recent Changes')
51+
it('should copy a page from plugins if nonexistant in db', async () => {
52+
return new Promise(resolve => {
53+
page.get('recent-changes', (e, got) => {
54+
if (e) throw e
55+
assert.equal(got.title, 'Recent Changes')
56+
resolve()
57+
})
4658
})
4759
})
4860
// note: here we assume the wiki-plugin-activity repo has been cloned into an adjacent directory
49-
it('should mark a page from plugins with the plugin name', () => {
50-
page.get('recent-changes', (e, got) => {
51-
if (e) throw e
52-
assert.equal(got.plugin, 'activity')
61+
it('should mark a page from plugins with the plugin name', async () => {
62+
return new Promise(resolve => {
63+
page.get('recent-changes', (e, got) => {
64+
if (e) throw e
65+
assert.equal(got.plugin, 'activity')
66+
resolve()
67+
})
5368
})
5469
})
55-
it('should create a page if it exists nowhere', () => {
56-
page.get(random(), (e, got) => {
57-
if (e) throw e
58-
assert.equal(got, 'Page not found')
70+
it('should create a page if it exists nowhere', async () => {
71+
return new Promise(resolve => {
72+
page.get(random(), (e, got) => {
73+
if (e) throw e
74+
assert.equal(got, 'Page not found')
75+
resolve()
76+
})
5977
})
6078
})
61-
it.skip('should eventually write the page to disk', async () => {
62-
const test = () => {
63-
console.log('should write', argv)
64-
fs.readFile(path.join(argv.db, 'asdf'), (err, data) => {
65-
if (err) throw err
66-
const readPage = JSON.parse(data)
67-
page.get('asdf', (e, got) => {
68-
assert.equal(readPage.title, got.title)
69-
})
79+
it('should eventually write the page to disk', async () => {
80+
return new Promise(resolve => {
81+
page.get('asdf', (e, got) => {
82+
if (e) throw e
83+
const page = JSON.parse(fs.readFileSync(path.join(path.sep, 'tmp', 'sfwtests', testid, 'pages', 'asdf')))
84+
assert.equal(got.title, page.title)
85+
resolve()
7086
})
71-
}
72-
if (page.isWorking()) {
73-
page.on('finished', () => test())
74-
} else {
75-
test()
76-
}
87+
})
7788
})
7889
})
7990
})

test/server.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const argv = require('../lib/defaultargs')({
1515
test: true,
1616
})
1717

18-
describe('server', d => {
18+
describe('server', () => {
1919
var app = {}
2020
let runningServer = null
2121
before(done => {
@@ -90,9 +90,9 @@ describe('server', d => {
9090
.then(
9191
() => {
9292
const page = JSON.parse(fs.readFileSync(loc))
93-
assert(page.story[1].id, 'a3')
94-
assert(page.story[2].id, 'a2')
95-
assert(page.journal[1].type, 'move')
93+
assert.equal(page.story[1].id, 'a3')
94+
assert.equal(page.story[2].id, 'a2')
95+
assert.equal(page.journal[1].type, 'move')
9696
},
9797
err => {
9898
throw err
@@ -137,7 +137,7 @@ describe('server', d => {
137137
.expect(200)
138138
.then(() => {
139139
const page = JSON.parse(fs.readFileSync(loc))
140-
assert(page.story.length == 4)
140+
assert.equal(page.story.length, 4)
141141
assert.equal(page.story[1].id, 'a3')
142142
assert.notEqual(page.story[2].id, 'a2')
143143
assert.equal(page.story[2].id, 'a5')
@@ -180,8 +180,8 @@ describe('server', d => {
180180
.expect(500)
181181
.then(() => {
182182
const page = JSON.parse(fs.readFileSync(loc))
183-
assert(page.story.length == 4)
184-
assert(page.journal.length == 5)
183+
assert.equal(page.story.length, 4)
184+
assert.equal(page.journal.length, 5)
185185
assert.equal(page.story[0].id, 'a1')
186186
assert.equal(page.story[3].text, 'this is the fourth paragraph')
187187
assert.equal(page.journal[4].type, 'edit')
@@ -217,7 +217,7 @@ describe('server', d => {
217217
.expect(200)
218218
.expect('Content-Type', /json/)
219219
.then(res => {
220-
assert(res.body.length == 1)
220+
assert.equal(res.body.length, 1)
221221
assert.equal(res.body[0], 'adsf-test-page')
222222
})
223223
.catch(err => {

0 commit comments

Comments
 (0)