If you are interested in contributing to this project, please read our general contribution guidelines first.
The purpose of this library is to facilitate the use of DSP-API (Knora) in web client development. It offers a convenient way to communicate with DSP-API without knowing specific technical details.
See design documentation.
For development, you need to install yalc
globally.
Run npm install yalc -g
. This will enable you to build and publish DSP-JS-LIB locally.
This library depends on RxJS, jsonld, and json2typescript.
jsonld
and json2typescript
are only used internally and listed as dependencies in package.json
.
RxJS
is listed as a peer dependency and not installed with npm install
.
It can be installed running npm run peer-deps
after npm install
.
RxJS
's Observable
is used in this library's public API
and has to be compatible with whatever version of RxJS
is used in the productive environment, e.g. an Angular application.
This library works with RxJS
's major version defined in package.json
. See Use of RxJS for details.
This package provides the following short-hand scripts:
npm run peer-deps
: Installs the project's peer dependencies. Peer dependencies are not installed withnpm install
, but have to be met before building or running the tests.npm run test
: Runs the library's tests defined in./karma.conf.js
.npm run prepare-dev-publication
: prepares a dev version of the library before building it. The dev version contains code to create mocked data to be used in unit tests of projects using DSP-JS-LIB.npm run build
: Builds the whole library without testing and puts the files into the./build/
folder.npm run yalc-publish
: Builds and publishes the package to the yalc app store (local publication of the lib).npm run yalc-publish-and-update
: Builds and publishes the package to the yalc app store (local publication of the lib) and updates any local repositories using the package with this newly created package.npm run npm-pack
: Tests and builds the library and then packs the./build/
folder into an NPM tgz package. The package is moved into a./dist/
folder.
Using yalc
, DSP-JS-LIB can be built and published locally:
- Run
npm install
andnpm run peer-deps
- Run
npm run prepare-dev-publication
if the mocks should be included, see section below. - Run
npm run yalc-publish
Alternatively, you can run npm run build-local
which combines the script mentioned above.
The development version contains mocks that produce tests data without a connection to DSP-API. These mocks were originally developed for internal use, but can be used to get data for unit tests in projects that use DSP-JS-LIB. The following example shows how to get a mocked resource without a connection to DSP-API:
import { MockResource } from "@dasch-swiss/dsp-js";
MockResource.getTestThing().subscribe(
(testthing: ReadResource) => {
console.log(testthing);
}
)
The mocks are configured in scripts/mock-exports.json
.
If you need a local version of this library that contains the mocks, do the following:
npm run prepare-dev-publication
to prepare a dev version.npm run yalc-publish
to publish a local build containing the mocks.
Note that prepare-dev-publication
modifies package.json
, tsconfig.json
and index.ts
.
Run this script only once and do not commit these changes.
Unit tests use data that is generated by DSP-API.
Download test data generated by DSP-API by running make get-test-data-from-release
.
This will download the test data for DSP-API release which is set in vars.mk
.
To integrate test data generated by DSP-API, the following scripts can be used. Instead of running these scripts one by one, you can also run make prepare-test-data
.
npm run integrate-system-test-data <path-to-generated-client-code>
: integrates generated test data for DSP-API system API.npm run integrate-admin-test-data <path-to-generated-client-code>
: integrates generated test data for DSP-API admin API.npm run integrate-v2-test-data <path-to-generated-client-code>
: integrates JSON-LD test data for DSP-API v2 API.npm run expand-jsonld-test-data
: creates versions with expanded prefixes for DSP-API v2 JSON-LD test data.
By default, all generated test data for the system and admin API is integrated with the corresponding script.
Test data for v2 has to be added to scripts/v2-test-data-config.json
to be used in the unit tests. source
refers to their location in DSP-API test data directory structure, destination
refers to their location in this repo.
All files that are listed in scripts/v2-test-data-config.json
will be copied to this library's test data directory when running npm run integrate-v2-test-data
.
Example:
{
"generated-test-data": [
{
"source": "/v2/ontologies/all-ontology-metadata-response.json",
"destination": "./test/data/api/v2/ontologies/all-ontology-metadata-response.json"
},
...
]
}
The example shown above will copy the file /v2/ontologies/all-ontology-metadata-response.json
from the generated test data to the specified destination in this library's test data folder.
In the unit tests, this file can then be used to mock request and response data.
When adding a new method to a v2 endpoint, only add the test data needed to test this method to facilitate the review process. Do not add v2 test data that is not used in the unit tests.
After integrating v2 test data, run npm run expand-jsonld-test-data
.
DSP-JS-LIB is compatible with a release of DSP-API that is specified in vars.mk
.
To update the target release of DSP-API, the following steps have to be carried out:
- Update DSP-API version in
vars.mk
, e.g., changev13.0.0
tov13.1.0
. - Download test-data from DSP-API release with
make get-test-data-from-release
. - Unpack generated test data and integrate it with
make prepare-test-data
. - Run the unit tests with
npm test
from the project root. - Check for differences in the generated test data with respect to the previous release of DSP-API. If there are changes in the test data that have no breaking effect, integrate them (add them to the git repo). Otherwise, DSP-JS has to be adapted to comply with the later version of DSP-JS. Also see section Integrating Generated Test Data from DSP-API.
- Run the e2e tests against the target release of DSP-API:
- prepare the local publication of the library using
npm run prepare-dev-publication
- build the library and publish it locally with
npm run yalc-publish
- change to directory
test-framework
- add the locally build library using
npm run yalc-add
and runnpm install
- run
npm run webdriver-update
and thennpm run e2e
- prepare the local publication of the library using
- See if the tests pass on GitHub CI.
Components have their own spec files defining unit tests. Since this library relies on DSP-API, static test data is generated from DSP-API and integrated automatically.
Actual calls to DSP-API in production are mocked with jasmine.Ajax
.
The data is read from the static test data files and passed to jasmine.Ajax
,
so the component being tested can react to it.
If a component A depends on a component B, then B has to be mocked during the test using a jasmine.Spy
.
The behavior of component B is simulated and a fake response is returned.
Mocking components turned out to be quite complex for the OntologyCache
.
Therefore, a stand-alone testing component has been made for this mock called MockOntology
that can be reused.
It can also be used to generate static test data in software projects using this library.
Since MockOntology
is complex, some global assertions are made to guarantee that the mock behaves like the actual component.
These assertions are defined in MockOntologyAssertions
.
./test-framework
provides a ready-to-use test environment for Angular developers.
The E2E test for DSP-JS-LIB are run by the test environment.
See the test framwork README for further instructions.