diff --git a/.eslintrc b/.eslintrc
index 8a78622b..abfa58a2 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -14,7 +14,8 @@
"experimentalObjectRestSpread": true,
"destructuring": true,
"spread": true
- }
+ },
+ "sourceType": "module"
},
"rules": {
// Possible Errors
diff --git a/src/help/__tests/browser-help.test.js b/src/help/__tests/help.browser.test.js
similarity index 72%
rename from src/help/__tests/browser-help.test.js
rename to src/help/__tests/help.browser.test.js
index 6003b7fb..b465f7b2 100644
--- a/src/help/__tests/browser-help.test.js
+++ b/src/help/__tests/help.browser.test.js
@@ -25,31 +25,25 @@ const mockDOM = () => {
describe('Help in Browser test suite', () => {
let mockIpcRenderer;
beforeEach(() => {
+ require('../../../bundles/help.preload');
mockIpcRenderer = {
send: jest.fn()
};
- window.preact = require('preact');
- window.html = require('htm').bind(window.preact.h);
- window.TopBar = require('../../components').topBar(window.html);
- window.APP_EVENTS = {closeDialog: 'close the dialog'};
- window.ELECTRONIM_VERSION = '1.33.7';
- window.docs = {
- 'Setup.md': 'Setup guide',
- 'Keyboard-shortcuts.md': 'Keyboard shortcuts',
- 'Troubleshooting.md': 'Troubleshooting'
- };
- window.ipcRenderer = mockIpcRenderer;
mockDOM();
+ window.ipcRenderer = mockIpcRenderer;
+ window.ELECTRONIM_VERSION = '1.33.7';
jest.isolateModules(() => {
- require('../browser-help');
+ require('../help.browser');
});
});
test('render, should render all documents', () => {
// Then
expect(document.querySelector('.toc-container').innerHTML)
.toMatch(/Table of Contents/);
- expect(document.querySelector('.documents-container').innerHTML)
- .toMatch(/Setup guide.+Keyboard shortcuts.+Troubleshooting/);
+ const documentsContent = document.querySelector('.documents-container').innerHTML;
+ expect(documentsContent).toContain('Setup
');
+ expect(documentsContent).toContain('Keyboard Shortcuts
');
+ expect(documentsContent).toContain('Troubleshooting
');
});
test('render, should show version in footer', () => {
// Then
@@ -62,7 +56,7 @@ describe('Help in Browser test suite', () => {
fireEvent.click(document.querySelector('.button.is-link.is-light'));
// Then
expect(mockIpcRenderer.send).toHaveBeenCalledTimes(1);
- expect(mockIpcRenderer.send).toHaveBeenCalledWith('close the dialog');
+ expect(mockIpcRenderer.send).toHaveBeenCalledWith('closeDialog');
});
});
});
diff --git a/src/help/__tests/index.test.js b/src/help/__tests/index.test.js
index 4590d595..3ebc2c49 100644
--- a/src/help/__tests/index.test.js
+++ b/src/help/__tests/index.test.js
@@ -13,8 +13,6 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
-const path = require('path');
-const DOCS_DIR = path.resolve(__dirname, '../../../docs');
describe('Help module test suite', () => {
let mockBrowserView;
let help;
@@ -28,31 +26,6 @@ describe('Help module test suite', () => {
}));
help = require('../');
});
- describe('fixRelativeUrls', () => {
- test('Combination of absolute and relative paths', () => {
- // Given
- const input = `
-
-
-
- `;
- // When
- const result = help.fixRelativeUrls(input);
- // Then
- expect(result).toBe(`
-
-
-
- `);
- });
- });
- test('loadDocs, should load object with documentation', () => {
- // When
- const docs = help.loadDocs();
- // Then
- expect(Object.keys(docs)).toContain('Setup.md');
- expect(docs['Setup.md']).toMatch(/There are several options available/i);
- });
test('openHelpDialog, should open dialog and add event listeners', () => {
// Given
mockBrowserView.setBounds = jest.fn();
diff --git a/src/help/__tests/preload.test.js b/src/help/__tests/preload.test.js
index 7e9a14df..49753135 100644
--- a/src/help/__tests/preload.test.js
+++ b/src/help/__tests/preload.test.js
@@ -16,41 +16,51 @@
const {waitFor} = require('@testing-library/dom');
describe('Help Module preload test suite', () => {
- let mockHelp;
beforeEach(() => {
- mockHelp = {loadDocs: jest.fn()};
jest.resetModules();
- jest.mock('../../main/preload', () => {
- global.mainPreloadLoaded = true;
- });
- jest.mock('../', () => mockHelp);
- });
- test('preload', () => {
- // When
- require('../preload');
- // Then
- expect(global.mainPreloadLoaded).toBe(true);
- expect(mockHelp.loadDocs).toHaveBeenCalledTimes(1);
- });
- test('bulma is loaded', async () => {
- // When
- require('../preload');
- window.document.body.innerHTML = '';
- // Then
- await waitFor(() => expect(document.querySelector('link[href*="bulma"]')).not.toBeNull());
});
- test('fontawesome is loaded', async () => {
- // When
- require('../preload');
- window.document.body.innerHTML = '';
- // Then
- await waitFor(() => expect(document.querySelector('link[href*="fontawesome"]')).not.toBeNull());
+ describe('preload (just for coverage and sanity, see bundle tests)', () => {
+ beforeEach(() => {
+ jest.mock('../help.browser.css', () => {});
+ jest.mock('!val-loader!./docs.browser.val-loader', () => ({
+ docs: {}
+ }), {virtual: true});
+ window.APP_EVENTS = {};
+ window.ELECTRONIM_VERSION = '1.33.7';
+ require('../preload');
+ });
+ test('adds required libraries', () => {
+ expect(window.ELECTRONIM_VERSION).toEqual('1.33.7');
+ });
});
- test('browser-help is loaded', async () => {
- // When
- require('../preload');
- window.document.body.innerHTML = '';
- // Then
- await waitFor(() => expect(document.querySelector('link[href*="browser-help"]')).not.toBeNull());
+ describe('preload.bundle', () => {
+ beforeEach(() => {
+ require('../../../bundles/help.preload');
+ });
+ test('loads styles in order', async () => {
+ // When
+ document.body.append(document.createElement('div'));
+ // Then
+ await waitFor(() => expect(document.head.children.length).toBeGreaterThan(0));
+ const styles = Array.from(document.querySelectorAll('style'));
+ expect(styles).toHaveLength(1);
+ expect(styles[0].innerHTML).toContain('.help-root {');
+ });
+ test('adds required libraries', () => {
+ expect(window.ELECTRONIM_VERSION).toEqual('0.0.0');
+ expect(window.preact).not.toBeUndefined();
+ expect(window.preactHooks).not.toBeUndefined();
+ expect(window.html).not.toBeUndefined();
+ expect(window.TopBar).not.toBeUndefined();
+ });
+ test('loads document contents with valid asset URLs', () => {
+ expect(window.docs).toEqual(expect.objectContaining({
+ 'Keyboard-shortcuts.md': expect.stringMatching(/Keyboard Shortcuts/i),
+ 'Roadmap.md': expect.any(String),
+ 'Screenshots.md': expect.stringContaining(''),
+ 'Setup.md': expect.stringMatching(/There are several options available/i),
+ 'Troubleshooting.md': expect.any(String)
+ }));
+ });
});
});
diff --git a/src/help/docs.browser.val-loader.js b/src/help/docs.browser.val-loader.js
new file mode 100644
index 00000000..2df7f374
--- /dev/null
+++ b/src/help/docs.browser.val-loader.js
@@ -0,0 +1,39 @@
+/*
+ Copyright 2019 Marc Nuri San Felix
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+ */
+const fs = require('fs');
+const path = require('path');
+const md = require('markdown-it')({html: true, xhtmlOut: true}); // NOSONAR
+
+const DOCS_DIR = path.resolve(__dirname, '..', '..', 'docs');
+// Should be a relative path from the src/help directory (or wherever index.html is located)
+const DOCS_DIR_RELATIVE = '../../docs';
+
+const fixRelativeUrls = s => s.replace(
+ /((src|href)\s*?=\s*?['"](?!http))([^'"]+)(['"])/gi,
+ `$1${DOCS_DIR_RELATIVE}/$3$4`
+);
+
+const loadDocs = () => fs.readdirSync(DOCS_DIR)
+ .filter(fileName => fileName.endsWith('.md'))
+ .reduce((acc, fileName) => {
+ acc[fileName] = fixRelativeUrls(md.render(fs.readFileSync(path.resolve(DOCS_DIR, fileName), 'utf8')));
+ return acc;
+ }, {});
+
+module.exports = () => ({
+ cacheable: true,
+ code: `module.exports = {docs: ${JSON.stringify(loadDocs())}};`
+});
diff --git a/src/help/browser-help.css b/src/help/help.browser.css
similarity index 100%
rename from src/help/browser-help.css
rename to src/help/help.browser.css
diff --git a/src/help/browser-help.js b/src/help/help.browser.js
similarity index 100%
rename from src/help/browser-help.js
rename to src/help/help.browser.js
diff --git a/src/help/index.html b/src/help/index.html
index 15a4e480..da82c99c 100644
--- a/src/help/index.html
+++ b/src/help/index.html
@@ -23,6 +23,8 @@
-
+
+
+