Skip to content

Commit 493493e

Browse files
committed
Add tests
1 parent e1087a2 commit 493493e

File tree

9 files changed

+6055
-150
lines changed

9 files changed

+6055
-150
lines changed

.npmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
registry=http://registry.npmjs.org/
2+
package-lock=false

.prettierrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"endOfLine": "lf",
3+
"semi": false,
4+
"singleQuote": false,
5+
"tabWidth": 2,
6+
"trailingComma": "es5",
7+
bracketSpacing: true,
8+
jsxBracketSameLine: true
9+
}

.travis.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
language: node_js
2+
cache: npm
3+
notifications:
4+
email: false
5+
node_js:
6+
- 10
7+
- 12
8+
- node
9+
install:
10+
- npm install
11+
script:
12+
- npm run lint
13+
- npm run test
14+
branches:
15+
only:
16+
- master
17+
- beta
18+
19+
jobs:
20+
include:
21+
# Define the release stage that runs semantic-release
22+
- stage: release
23+
node_js: lts/*
24+
# Advanced: optionally overwrite your default `script` step to skip the tests
25+
# script: skip
26+
deploy:
27+
provider: script
28+
skip_cleanup: true
29+
script:
30+
- npx semantic-release
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"data": [
3+
{
4+
"type": "menu_link_content--menu_link_content",
5+
"id": "jsonapi_menu_test.user.login",
6+
"attributes": {
7+
"description": "Login.",
8+
"enabled": true,
9+
"expanded": false,
10+
"menu_name": "jsonapi_menu_items_test",
11+
"meta": [],
12+
"options": [],
13+
"parent": "",
14+
"provider": "jsonapi_menu_items_test",
15+
"route": {
16+
"name": "user.login",
17+
"parameters": []
18+
},
19+
"title": "Login",
20+
"url": "\/api\/user\/login",
21+
"weight": "0"
22+
}
23+
},
24+
{
25+
"type": "menu_link_content--menu_link_content",
26+
"id": "jsonapi_menu_test.open",
27+
"attributes": {
28+
"description": "Home.",
29+
"enabled": true,
30+
"expanded": false,
31+
"menu_name": "jsonapi_menu_items_test",
32+
"meta": [],
33+
"options": [],
34+
"parent": "",
35+
"provider": "jsonapi_menu_items_test",
36+
"route": {
37+
"name": "menu_test.menu_name_test",
38+
"parameters": []
39+
},
40+
"title": "Home",
41+
"url": "\/api\/menu_name_test",
42+
"weight": "10"
43+
}
44+
},
45+
{
46+
"type": "menu_link_content--menu_link_content",
47+
"id": "menu_link_content:108a993a-25dd-4d76-ac8d-49ae76ac5231",
48+
"attributes": {
49+
"description": null,
50+
"enabled": true,
51+
"expanded": false,
52+
"menu_name": "jsonapi_menu_items_test",
53+
"meta": {
54+
"entity_id": "1"
55+
},
56+
"options": [],
57+
"parent": "jsonapi_menu_test.open",
58+
"provider": "menu_link_content",
59+
"route": {
60+
"name": "menu_test.menu_callback_title",
61+
"parameters": []
62+
},
63+
"title": "an item",
64+
"url": "\/api\/menu_callback_title",
65+
"weight": "0"
66+
}
67+
}
68+
]
69+
}

__tests__/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require('jest-fetch-mock').enableMocks()
2+
3+
const { sourceNodes } = require(`../gatsby-node`)
4+
5+
describe(`gatsby-source-drupal-menu-links`, () => {
6+
let nodes = {}
7+
const parents = new Map();
8+
const baseUrl = `http://fixture`
9+
const createContentDigest = jest.fn().mockReturnValue(`contentDigest`)
10+
const actions = {
11+
createNode: jest.fn(node => (nodes[node.id] = node)),
12+
createParentChildLink: jest.fn(({parent, child}) => (parents.set(child.id, parent.id))),
13+
}
14+
15+
const reporter = {
16+
info: jest.fn(),
17+
activityTimer: jest.fn(() => activity),
18+
log: jest.fn(),
19+
}
20+
21+
const args = {
22+
createContentDigest,
23+
actions,
24+
reporter,
25+
}
26+
27+
beforeAll(async () => {
28+
const fixtureContents = require(`./fixtures/menu_items__jsonapi_menu_test.json`)
29+
fetchMock.mockOnce(JSON.stringify(fixtureContents))
30+
await sourceNodes(args, {baseUrl, menus: ['jsonapi_menu_items_test']})
31+
})
32+
33+
it(`Generates nodes`, () => {
34+
expect(Object.keys(nodes).length).toEqual(3)
35+
expect(nodes[`menu-items-jsonapi_menu_test.user.login`]).toBeDefined()
36+
expect(nodes[`menu-items-jsonapi_menu_test.open`]).toBeDefined()
37+
expect(nodes[`menu-items-menu_link_content:108a993a-25dd-4d76-ac8d-49ae76ac5231`]).toBeDefined()
38+
})
39+
40+
it(`Nodes contain attributes data`, () => {
41+
expect(nodes[`menu-items-menu_link_content:108a993a-25dd-4d76-ac8d-49ae76ac5231`].title).toEqual('an item')
42+
})
43+
44+
it(`Associates with childen`, () => {
45+
expect(nodes[`menu-items-menu_link_content:108a993a-25dd-4d76-ac8d-49ae76ac5231`].parent).toEqual('menu-items-jsonapi_menu_test.open')
46+
expect(parents.get(`menu-items-menu_link_content:108a993a-25dd-4d76-ac8d-49ae76ac5231`)).toEqual(`menu-items-jsonapi_menu_test.open`)
47+
})
48+
})

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
clearMocks: true,
3+
coverageDirectory: "coverage",
4+
testMatch: ['<rootDir>/__tests__/*.js'],
5+
};

package.json

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
{
22
"name": "gatsby-source-drupal-menu-links",
3-
"version": "1.0.0",
3+
"version": "0.0.0-development",
44
"description": "Integrates Drupal's jsonapi_menu_items module with Gatsby",
55
"main": "index.js",
66
"scripts": {
7+
"build": "babel src --out-dir . --ignore \"**/__tests__\"",
8+
"prepare": "cross-env NODE_ENV=production npm run build",
9+
"watch": "babel -w src --out-dir . --ignore \"**/__tests__\"",
10+
"semantic-release": "semantic-release",
11+
"format": "prettier --write \"{test,src}/**/*.js\"",
12+
"lint": "prettier --check \"{test,src}/**/*.js\"",
713
"test": "jest"
814
},
915
"repository": {
1016
"type": "git",
11-
"url": "git+https://github.com/larowlan/gatsby-source-drupal-menu-links.git"
17+
"url": "https://github.com/larowlan/gatsby-source-drupal-menu-links.git"
1218
},
1319
"keywords": [
1420
"drupal",
@@ -23,7 +29,11 @@
2329
"@babel/cli": "^7.10.3",
2430
"@babel/core": "^7.10.3",
2531
"babel-preset-gatsby-package": "^0.5.2",
26-
"cross-env": "^5.2.1"
32+
"cross-env": "^5.2.1",
33+
"semantic-release": "^17.1.1",
34+
"prettier": "^2.0.4",
35+
"jest-fetch-mock": "^3.0.3",
36+
"jest": "^26.4.2"
2737
},
2838
"dependencies": {
2939
"@babel/runtime": "^7.10.3",
@@ -36,11 +46,6 @@
3646
"url": "https://github.com/larowlan/gatsby-source-drupal-menu-links/issues"
3747
},
3848
"homepage": "https://github.com/larowlan/gatsby-source-drupal-menu-links#readme",
39-
"scripts": {
40-
"build": "babel src --out-dir . --ignore \"**/__tests__\"",
41-
"prepare": "cross-env NODE_ENV=production npm run build",
42-
"watch": "babel -w src --out-dir . --ignore \"**/__tests__\""
43-
},
4449
"engines": {
4550
"node": ">=10.13.0"
4651
}

src/gatsby-node.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
require('es6-promise').polyfill();
2-
require('isomorphic-fetch');
1+
require("es6-promise").polyfill()
2+
require("isomorphic-fetch")
33

44
exports.sourceNodes = async (
55
{
@@ -19,36 +19,40 @@ exports.sourceNodes = async (
1919
) => {
2020
const {
2121
baseUrl,
22-
apiBase = 'jsonapi',
22+
apiBase = "jsonapi",
2323
basicAuth,
2424
headers,
2525
menus,
2626
} = pluginOptions
27-
const {createNode, createParentChildLink} = actions
27+
const { createNode, createParentChildLink } = actions
2828

2929
reporter.info(`Starting to fetch menu link items from Drupal`)
30-
reporter.info('Menus to fetch are ' + menus.join(', '))
30+
reporter.info("Menus to fetch are " + menus.join(", "))
3131

3232
// Data can come from anywhere, but for now create it manually
33-
const menuResponses = await Promise.all(menus.map(async menu => {
34-
return await fetch(`${baseUrl}/${apiBase}/menu_items/${menu}`, {
35-
auth: basicAuth,
36-
headers
37-
}).then(function (response) {
38-
if (response.status >= 400) {
39-
reporter.error(`Bad response from ${baseUrl}/${apiBase}/menu_items/${menu}`);
40-
}
41-
return response.json();
33+
const menuResponses = await Promise.all(
34+
menus.map(async (menu) => {
35+
return await fetch(`${baseUrl}/${apiBase}/menu_items/${menu}`, {
36+
auth: basicAuth,
37+
headers,
38+
}).then(function (response) {
39+
if (response.status >= 400) {
40+
reporter.error(
41+
`Bad response from ${baseUrl}/${apiBase}/menu_items/${menu}`
42+
)
43+
}
44+
return response.json()
45+
})
4246
})
43-
}));
44-
menuResponses.forEach(({data: menuItems}) => {
45-
const map = new Map();
47+
)
48+
menuResponses.forEach(({ data: menuItems }) => {
49+
const map = new Map()
4650
menuItems.forEach((item) => {
47-
const nodeContent = JSON.stringify(item);
48-
const id = `menu-items-${item.id}`;
49-
let parentId = null;
51+
const nodeContent = JSON.stringify(item)
52+
const id = `menu-items-${item.id}`
53+
let parentId = null
5054
if (item.attributes.parent) {
51-
parentId = `menu-items-${item.attributes.parent}`;
55+
parentId = `menu-items-${item.attributes.parent}`
5256
}
5357
const nodeMeta = {
5458
id,
@@ -58,17 +62,15 @@ exports.sourceNodes = async (
5862
type: `MenuItems`,
5963
mediaType: `text/html`,
6064
content: nodeContent,
61-
contentDigest: createContentDigest(item)
62-
}
63-
};
64-
const node = Object.assign({}, item.attributes, nodeMeta);
65-
createNode(node);
65+
contentDigest: createContentDigest(item),
66+
},
67+
}
68+
const node = Object.assign({}, item.attributes, nodeMeta)
69+
createNode(node)
6670
if (parentId && map.has(parentId)) {
67-
createParentChildLink({parent: map.get(parentId), child: node})
71+
createParentChildLink({ parent: map.get(parentId), child: node })
6872
}
6973
map.set(id, node)
7074
})
71-
});
72-
75+
})
7376
}
74-

0 commit comments

Comments
 (0)