Skip to content

Commit 1dc4aa9

Browse files
TG199mark-wiemer
andauthored
docs: migrate shared behaviours to docs-next (#5432)
Co-authored-by: Mark Wiemer <markwiemer@outlook.com>
1 parent ab256b8 commit 1dc4aa9

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

docs-next/astro.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export default defineConfig({
102102
label: "Run cycle overview",
103103
slug: "explainers/run-cycle-overview",
104104
},
105+
{
106+
label: "Shared behaviours",
107+
slug: "explainers/shared-behaviours",
108+
},
105109
{ label: "Test duration", slug: "explainers/test-duration" },
106110
{
107111
label: "Test fixture decision tree",
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
description: Reuse tests across contexts with `this`
3+
title: Shared Behaviours
4+
---
5+
6+
Mocha currently has no concept of a "shared behaviour" however the "contexts" facilitate this feature. For example suppose you have an `Admin` which inherits from `User`, you most likely will not want to duplicate the `User` tests for `Admin`. The "context" (`this`) is the same object within the "before each", "after each" hooks, and the test-case itself, allowing you to utilize this instead of closures to store data. The following is an example of how you can achieve this sort of functionality:
7+
8+
shared.js:
9+
10+
```js
11+
exports.shouldBehaveLikeAUser = function() {
12+
it('should have .name.first', function() {
13+
this.user.name.first.should.equal('tobi');
14+
})
15+
16+
it('should have .name.last', function() {
17+
this.user.name.last.should.equal('holowaychuk');
18+
})
19+
20+
describe('.fullname()', function() {
21+
it('should return the full name', function() {
22+
this.user.fullname().should.equal('tobi holowaychuk');
23+
})
24+
})
25+
};
26+
```
27+
28+
test.js:
29+
30+
```js
31+
var User = require('./user').User
32+
, Admin = require('./user').Admin
33+
, shared = require('./shared');
34+
35+
describe('User', function() {
36+
beforeEach(function() {
37+
this.user = new User('tobi', 'holowaychuk');
38+
})
39+
40+
shared.shouldBehaveLikeAUser();
41+
})
42+
43+
describe('Admin', function() {
44+
beforeEach(function() {
45+
this.user = new Admin('tobi', 'holowaychuk');
46+
})
47+
48+
shared.shouldBehaveLikeAUser();
49+
50+
it('should be an .admin', function() {
51+
this.user.admin.should.be.true;
52+
})
53+
})
54+
```
55+
56+
user.js:
57+
58+
```js
59+
exports.User = User;
60+
exports.Admin = Admin;
61+
62+
function User(first, last) {
63+
this.name = {
64+
first: first,
65+
last: last
66+
};
67+
}
68+
69+
User.prototype.fullname = function() {
70+
return this.name.first + ' ' + this.name.last;
71+
};
72+
73+
function Admin(first, last) {
74+
User.call(this, first, last);
75+
this.admin = true;
76+
}
77+
78+
Admin.prototype.__proto__ = User.prototype;
79+
```

docs/api-tutorials/jsdoc.tutorials.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
},
55
"spies": {
66
"title": "Spies"
7+
},
8+
"shared-behaviours": {
9+
"title": "Shared behaviours"
710
}
811
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
Mocha currently has no concept of a "shared behaviour" however the "contexts" facilitate this feature. For example suppose you have an `Admin` which inherits from `User`, you most likely will not want to duplicate the `User` tests for `Admin`. The "context" (`this`) is the same object within the "before each", "after each" hooks, and the test-case itself, allowing you to utilize this instead of closures to store data. The following is an example of how you can achieve this sort of functionality:
2+
3+
shared.js:
4+
5+
```js
6+
exports.shouldBehaveLikeAUser = function() {
7+
it('should have .name.first', function() {
8+
this.user.name.first.should.equal('tobi');
9+
})
10+
11+
it('should have .name.last', function() {
12+
this.user.name.last.should.equal('holowaychuk');
13+
})
14+
15+
describe('.fullname()', function() {
16+
it('should return the full name', function() {
17+
this.user.fullname().should.equal('tobi holowaychuk');
18+
})
19+
})
20+
};
21+
```
22+
23+
test.js:
24+
25+
```js
26+
var User = require('./user').User
27+
, Admin = require('./user').Admin
28+
, shared = require('./shared');
29+
30+
describe('User', function() {
31+
beforeEach(function() {
32+
this.user = new User('tobi', 'holowaychuk');
33+
})
34+
35+
shared.shouldBehaveLikeAUser();
36+
})
37+
38+
describe('Admin', function() {
39+
beforeEach(function() {
40+
this.user = new Admin('tobi', 'holowaychuk');
41+
})
42+
43+
shared.shouldBehaveLikeAUser();
44+
45+
it('should be an .admin', function() {
46+
this.user.admin.should.be.true;
47+
})
48+
})
49+
```
50+
51+
user.js:
52+
53+
```js
54+
exports.User = User;
55+
exports.Admin = Admin;
56+
57+
function User(first, last) {
58+
this.name = {
59+
first: first,
60+
last: last
61+
};
62+
}
63+
64+
User.prototype.fullname = function() {
65+
return this.name.first + ' ' + this.name.last;
66+
};
67+
68+
function Admin(first, last) {
69+
User.call(this, first, last);
70+
this.admin = true;
71+
}
72+
73+
Admin.prototype.__proto__ = User.prototype;
74+
```

0 commit comments

Comments
 (0)