Skip to content

Commit

Permalink
Make debugger work
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Lenoch committed Oct 12, 2018
1 parent bad2ae6 commit 6d36b2e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"presets": [ "@babel/preset-env" ],
"plugins": [
"@babel/plugin-transform-regenerator"
]
],
"sourceMaps": "both"
}
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"--runInBand",
"--no-cache"
],
"sourceMaps": false,
"sourceMaps": true,
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"windows": {
Expand Down
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
},
"dependencies": {
"change-case": "^3.0.2",
"class-transformer": "^0.1.9",
"gatsby": "^2.0.11",
"kentico-cloud-delivery": "^4.3.0",
"lodash": "^4.17.11",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.3.3"
},
"peerDependencies": {
Expand Down
13 changes: 11 additions & 2 deletions src/__tests__/__snapshots__/normalize.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`createContentItemNode with correct arguments should creates a content item node 1`] = `
exports[`createContentItemNode with correct arguments creates a content item node 1`] = `
Object {
"children": Array [],
"contentType___NODE": "aea6da0c-4130-593c-8b6e-006e6bace1de",
Expand Down Expand Up @@ -65,7 +65,7 @@ Object {
}
`;

exports[`createContentTypeNode with correct arguments should creates a content type node 1`] = `
exports[`createContentTypeNode with correct arguments creates a content type node 1`] = `
Object {
"children": Array [],
"contentItems___NODE": Array [],
Expand Down Expand Up @@ -111,3 +111,12 @@ Object {
"usedByContentItems___NODE": Array [],
}
`;

exports[`toJson with correct arguments converts class instances to plain objects recursively 1`] = `
Object {
"number": 20,
"property": Object {
"text": "Test 01",
},
}
`;
29 changes: 27 additions & 2 deletions src/__tests__/normalize.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const contentItem = require('./contentItem.json');
// const contentItemNodes = require('./contentItemNodes');

describe('createContentTypeNode with correct arguments', () => {
it(`should creates a content type node`, () => {
it(`creates a content type node`, () => {
const createNodeId = jest.fn();
createNodeId.mockReturnValue(`aea6da0c-4130-593c-8b6e-006e6bace1de`);

Expand All @@ -17,7 +17,7 @@ describe('createContentTypeNode with correct arguments', () => {
});

describe('createContentItemNode with correct arguments', () => {
it(`should creates a content item node`, () => {
it(`creates a content item node`, () => {
const createNodeId = jest.fn();
createNodeId.mockReturnValue(`362bd0da-5b1a-533b-9575-107c2e3c6931`);

Expand All @@ -28,3 +28,28 @@ describe('createContentItemNode with correct arguments', () => {
).toMatchSnapshot();
});
});


describe('toJson with correct arguments', () => {
it(`converts class instances to plain objects recursively`, () => {
let PropertyClass = class {
constructor(text) {
this.text = text;
}
};

let ParentClass = class {
constructor(property, number) {
this.property = property;
this.number = number;
}
};

let property = new PropertyClass(`Test 01`);
let parent = new ParentClass(property, 20);

expect(
normalize.toJson(parent)
).toMatchSnapshot();
});
});
14 changes: 12 additions & 2 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require(`@babel/polyfill`);
const _ = require(`lodash`);
// const classToPlain = require(`class-transformer`);
const deliveryClient = require(`kentico-cloud-delivery`);
const normalize = require(`./normalize`);

Expand All @@ -14,13 +15,22 @@ kcProjectId: ${kcProjectId}, kcLanguageCodenames: ${kcLanguageCodenames}.`);
});

const contentTypesResponse = await client.types().getPromise();
// let contentTypes = classToPlain(contentTypesResponse.types);

let contentTypeNodes = contentTypesResponse.types.map((contentType) =>
let contentTypes = contentTypesResponse.types.map((contentType) =>
normalize.toJson(contentType)
);

let contentTypeNodes = contentTypes.map((contentType) =>
normalize.createContentTypeNode(createNodeId, contentType)
);

const contentItemsResponse = await client.items().getPromise();
const contentItems = contentItemsResponse.items;
// let contentItems = classToPlain(contentItemsResponse.items);

let contentItems = contentItemsResponse.items.map((contentItem) =>
normalize.toJson(contentItem)
);

normalize.refillRichTextModularCodenames(
contentItems, contentItemsResponse.debug.response.data.items
Expand Down
25 changes: 25 additions & 0 deletions src/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,29 @@ const addModularItemLinks = (itemNode, linkedNodes, linkPropertyName) => {
);
};

const isObject = (val) => {
if (val === null) {
return false;
}

return ((typeof val === `function`) || (typeof val === `object`));
};

const toJson = (entity) => {
if (isObject(entity)) {
let jsoned = {};

Object.keys(entity).forEach((key) => {
const val = entity[key];
jsoned[key] = toJson(val);
});

return jsoned;
} else {
return entity;
}
};

exports.createContentTypeNode = createContentTypeNode;
exports.createContentItemNode = createContentItemNode;
exports.decorateTypeNodesWithItemLinks = decorateTypeNodesWithItemLinks;
Expand All @@ -322,3 +345,5 @@ exports.decorateItemNodeWithModularElementLinks

exports.decorateItemNodeWithRichTextModularLinks
= decorateItemNodeWithRichTextModularLinks;

exports.toJson = toJson;

0 comments on commit 6d36b2e

Please sign in to comment.