ui5-simple-require
Description
Import UI5 modules into NodeJS applications, allowing isolation of UI5 components and injection of dependencies to create an isolated test environment.
Requirements
- NodeJS, version 8.0 or higher
Installation
Install at your Node.js project:
$ npm install ui5-simple-require --save-dev
Usage
When importing UI5 modules, there are few things to consider, the two major things that need to be resolved are SAP's Global Context and Dependency Lookup. This tool provides ways to inject both dependencies based on their path as well as properties that will be injected in global context when the module is loaded.
Require UI5 Modules
ui5require
will load modules that are in sap.ui.define
style. For example, suppose you have the following UI5 module:
sapui;
When running the import code:
const ui5require = ui5require;const MyClass = ; var myObject = ;
The constant variable MyClass
will contain the constructor function.
Resolving Dependencies
Suppose a similar module now with defined dependencies.
sapui;
Dependencies will be resolved in the following order:
- Position injected
- Path injected
- Loaded
Position Injected
const MyClass = ;
Path Injected
Injecting by path will create a virtual path lookup. Where, whenever a dependency is required, it will first look at this injected path.
const moduleLoader = ;const ui5require = moduleLoaderui5require; moduleLoader;const MyClass = ;
Loaded
When no injection methods are passed, module loader will try to load the module by its path. Meaning that if a file that matches /path/to/Dependency
is found in the path, the original dependency is loaded.
Injecting Global Dependency
Injecting a global dependency works similar to injecting dependencies. A global lookup is created for whenever a new module is loaded a global context is injected.
const moduleLoader = ;const ui5require = moduleLoaderui5require; moduleLoader;const MyClass = ;
The injected object will be under global.sap
object. Such as:
// ... ui5 modulesapsomeValue; // evaluates to "custom value"
Loading UI5 Libraries
You can use the importLib
function to load UI5-style libraries. ui5-simple-require populates the namespace of the library in the global context with an empty object. If you need a more elaborate implementation, you can inject using the globalContext
function.
// ./test/example/mockLib/com/sap/mockLib/library.jssapui
Example
Using mocha and chai for writting unit tests.
// MoneyChanger.jssapui; ************************************************************// MoneyChangerTest.jsconst expect = expect; const API = ;const ui5require = ui5require; API; const MoneyChanger = ;const Note = ;const Coin = ; ;
API
ui5require(path [, position_dependencies] [, global_context])
path
<string>position_dependencies
<Array>global_context
<Object>- Returns: <Object> Loaded Module.
inject(path, dependency)
path
<string>dependency
<Object>
clearInjection()
Deletes any dependencies passed with inject
globalContext(context)
context
<string>
clearGlobalContext()
Deletes any global object passed with globalContext(...)
importLib(libRootPath, libFilePath, libUI5Namespace)
Import UI5 libraries
libRootPath
<string>libFilePath
<string>libUI5Namespace
<string>- Returns: <Object> Loaded UI5 library module.
createExtendableFromPrototype(prototype)
prototype
<Object>- Returns: <Object> prototype with UI5's fake
extend
method.
createExtendableFromObj(prototype)
prototype
<string>- Returns: <Object> class with UI5's fake
extend
method.
[DEPRECATED] import(path [, position_dependencies] [, global_context])
path
<string>position_dependencies
<Array>global_context
<Object>- **Returns: ** <Object> Loaded Module.
How to obtain support
If you think you found a bug or need help using the module, please create a new github issue.
This project is provided "as-is", with no expected changes or support.
License
Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.