A NodeJS NPM module that contains the client code for the global header including level 1-3 navigation and enables sharing of common assets in this module between code bases. These code bases are currently:
This is a temporary step to extract original FEAR header code in to a self contained reusable module. Also to migrate from deprecated fear-core-ui to mns-core-ui. This module also has no dependency on a javascript framework.
UPDATE: The module no longer has dependency on fear-core-ui as a result of https://github.com/DigitalInnovation/mns-core-ui-header/pull/92.
For more info:
Install the module in your application npm install mns-core-ui-header --save
The following steps would usually be performed as part of a build process:
- copy assets from
src/mns-core-ui-header/templates/header/assets
to desired location in your application. - setup templates (see templates section below)
- define paths to assets used in styling (see SASS section below)
- import css bundle
@import 'templates/header/bundle';
- include the javascript bundle:
- If using es6
import Header from 'mns-core-ui-header/dist/templates/header/bundle';
- If using es5
define([
'mns-core-ui-header'
], function (Header) {});
- The header needs to be instantiated:
new Header.Top();
new Header.Navigation();
- The level2 and level3 markup should be made available server side to the html templates as
globalHeader.topNavLevel3
(A subsequent client side call is made in addition to this so that any recent content changes appear in the page navigation)
Templates:
The src folder contains the partials needed for the header and these partials have a .hbs
(handlebars) extension for consumption in newer M&S codebases (e.g FESK projects).
The partials are made available also in the dist folder with a .html
extension for consumption in legacy codebases (e.g FEAR) that require templates to have this extension.
It is important to note is that currently the partials only use mustache syntax (i.e the engine used in FEAR) and hence are compatible with handlebars template engines as handlebars is a superset of mustache. Care should be taken if introducing handlebars functionality to the templates whilst they are being consumed by a FEAR page for this reason.
To use templates in your application (FESK)
- the path path/to/node/modules +
mns-core-ui-header/src/mns-core-ui-header/templates/header/partials
needs to be added to your handlebars partialsDir config property (see docs path) - assign configuration data to the page handlebars handler.
const headerService = require('mns-fe-unicorn-platform/lib/services/getHeaderContent.js');
import configuration from 'path/to/fesk-configuration';
res.render('yourPage', {
header: headerService.getConfig(header.config, configuration)
});
- include the following in your page template
{{> templates/header/partials/default/header header }}
Images
- define
$mns-core-ui-header-images-dir
variable that contains path to where assets have been copied to in above steps @import 'mns-core-ui-header/templates/header/sass/module_images';
Fonts
- define
$mns-london-font-path
variable with path to font asset location e.g mns-core-ui/dist/fonts/mns-london
- define
$mns-london-font-name
e.g mns-london
@import 'mns-core-ui/globals/typography/fonts/mns-london/module_mns-london';
Font-icons
- define
$mns-core-ui-header-icon-font-family
variable e.g mns-icons
- define
$mns-core-ui-header-fonts-dir
e.g ~mns-core-ui/dist/fonts/
@import 'mns-core-ui-header/templates/header/sass/module_font-icons';
Configuration
The module exposes a config object:
require('mns-core-ui-header).config
There is core configuration of the header (data) and the associated key (key). This is data that is deemed to not need business control.
* core configuration require('mns-core-ui-header).config.data
- contains link data for flags and dropdowns.
A decorators object is also exposed, so this data can be optionally added the full config object. This is data that does/could need business control and could be replaced by an API when thats implemented. Current decorators are:
* labels - labels for dropdown menu items.
For detail on above see /index.js
Events
There are some events that can/should be fired from a client page/application that the header listens to.
- Update the header mini bag count. event name
updateItemCount
passing an integer of new page count in variable myBagCount
i.e event.myBagCount
Development
The development environment is based on mns-fe-starter-kit and documentation for how that works can be found here
Mocking Routes
Mocked endpoints can be added to your express app by using the ExposeRoutes functionality. Add the following lines to your server.js file. Note that the "ExposeRoutes" line must be added above the current routes that your app defines.
const indexFile = require('../../index');
const ExposeRoutes = indexFile.ExposeRoutes;
server = new ExposeRoutes(server).attachToServer();
You must also make sure that your config file containing the api endpoints is setup with the following:
"loyaltyService": "http://127.0.0.1:3002/loyalty-service/"
"loyaltyServiceStub": "http://127.0.0.1:3002/loyalty-service/"
Testing
To run the UI tests locally:
- In terminal tab start the webapp
npm start
- In another terminal tab
npm run test:ui
This will test against localhost - Note that UI test spec files must have the the following pattern in order to be picked up ./test/specs/**/*test.spec.js
To run the UI tests in the cloud against a range of browsers and connect to your localhost:
- In terminal tab start the webapp
npm start
- Add you sauce user credentials to the wdio.sauce.conf.js file, or set them as environment variables SAUCE_USER and SAUCE_KEY
- In another terminal tab
npm run test:ui:sauce
- You can configure the required browsers in /test/wdio.sauce.conf.js
Visual Tests Admin Panel
- The visual tests are configured to use the 'Admin Panel' (http://webdrivercss-admin.dev.apps.mnscorp.net/)
- When the test starts it will first try to pull a baseline matching the id that you supplied
- When the test ends it will push the results to the admin panel so that you can view the results
- If this is the first time running the tests you must use the --capture (--c) flag to capture a baseline and push to the Admin panel
To run the Visual tests locally:
- In terminal tab start the webapp
npm start
- In another terminal type: npm run test:visual -- --id myid (--c if first time)
- Note that visual test spec files must have the the following pattern in order to be picked up ./test/specs/**/*css.spec.js
To run the Visual tests against sauce labs:
- In terminal tab start the webapp
npm start
- Add your sauce user credentials to the wdio.css.sauce.conf.js file, or set them as environment variables SAUCE_USER and SAUCE_KEY
- In another terminal type: npm run test:visual:sauce -- --id myid
- In another terminal tab
npm run test:visual:sauce
- You can configure the required browsers in /test/wdio.sauce.css.conf.js
To run the Visual tests against another id:
- In terminal tab start the webapp
npm start
- In another terminal type: npm run test:visual -- -i myid -l anotherid
This will pull the baseline from another id and push the results to your id
Publishing
Shrinkwrap the module
(https://github.com/DigitalInnovation/fesk-documentation/blob/master/how_tos/shrinkwrap_npm_dependencies.md)
Publish the module to NPM
(https://confluence.platform.mnscorp.net/display/FF/publishing+npm+modules)