-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
124 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
= Client side | ||
:sourcedir: ../ | ||
|
||
== Install Node modules | ||
This chapter covers setting up and running client-side tests | ||
|
||
[source, shell] | ||
== Node modules | ||
|
||
For this example, we're going to need some more modules. | ||
|
||
[source, Terminal] | ||
---- | ||
npm install --save-dev react react-dom react-test-renderer @types/react | ||
---- | ||
|
@@ -14,7 +18,7 @@ npm install --save-dev react react-dom react-test-renderer @types/react | |
|
||
== Source code | ||
|
||
Let's write a test for the following functional React component: | ||
Here is the react component we will be testing: | ||
|
||
.src/main/resources/assets/js/HelloWorld.tsx | ||
[source, typescript] | ||
|
@@ -24,14 +28,48 @@ include::{sourcedir}src/main/resources/assets/js/HelloWorld.tsx[] | |
|
||
== Test | ||
|
||
Adding the following test will let us test the component: | ||
|
||
.src/jest/client/HelloWorld.test.tsx | ||
[source, typescript] | ||
---- | ||
include::{sourcedir}src/jest/client/HelloWorld.test.tsx[] | ||
---- | ||
|
||
== Output | ||
|
||
Run the test, and see what happens: | ||
|
||
npm test src/jest/client/HelloWorld.test.tsx | ||
|
||
[source, Terminal] | ||
---- | ||
> [email protected] test | ||
> jest --no-cache --coverage src/jest/client/HelloWorld.test.tsx | ||
PASS CLIENT src/jest/client/HelloWorld.test.tsx | ||
HelloWorld | ||
✓ should render (11 ms) | ||
----------------|---------|----------|---------|---------|------------------- | ||
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ||
----------------|---------|----------|---------|---------|------------------- | ||
All files | 100 | 100 | 100 | 100 | | ||
HelloWorld.tsx | 100 | 100 | 100 | 100 | | ||
----------------|---------|----------|---------|---------|------------------- | ||
Test Suites: 1 passed, 1 total | ||
Tests: 1 passed, 1 total | ||
Snapshots: 0 total | ||
Time: 2.1 s | ||
Ran all test suites matching /src\/jest\/client\/HelloWorld.test.tsx/i. | ||
---- | ||
|
||
|
||
== Summary | ||
|
||
In this guide, we have learned how to write a test for a React component using Jest and React Test Renderer. | ||
You have now seen how to write a test for a React component using Jest and React Test Renderer. | ||
|
||
Now that we have some tests, it may be useful to store coverage reports in a tool that can show progress over time. | ||
One such tool is Codecov. | ||
|
||
Now that there exists some tests, it may be useful to store coverage reports in a tool that can show progress over time. One such tool is Codecov. The following tutorial will show you how to integrate link:coverage[Codecov] with your project. | ||
The following chapter will show you how to integrate link:coverage[Codecov] with your project. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
= Testing XP apps with Jest | ||
= Testing with Jest and Mock XP | ||
:toc: right | ||
:experimental: | ||
:sourcedir: ../ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,88 @@ | ||
= Mock XP | ||
:sourcedir: ../ | ||
|
||
In this chapter we introduce the Mock XP library. | ||
In this chapter we introduce Mock XP | ||
|
||
== Introduction | ||
|
||
When it comes to mocking more complex things (such as interaction with Content and Node APIs) Enonic has a library called `@enonic/mock-xp` that can help you with some of the heavy lifting. | ||
When it comes to mocking more complex things (such as interaction with Content and Node APIs) Enonic has an NPM module called `@enonic/mock-xp` that can help you with some of the heavy lifting. | ||
|
||
== Install Mock XP | ||
|
||
Run the following command to install it: | ||
|
||
[source, shell] | ||
---- | ||
npm install --save-dev @enonic/mock-xp | ||
---- | ||
|
||
== Source code | ||
|
||
Let's add a test for the following controller: | ||
Let's use the following controller file: | ||
|
||
.src/main/resources/controllers/preview.ts | ||
[source, typescript] | ||
[source, TypeScript] | ||
---- | ||
include::{sourcedir}src/main/resources/controllers/preview.ts[] | ||
---- | ||
|
||
== Tests | ||
|
||
In this test `mock-xp` is used to set up a project repo, a person folder, a couple of images, and a couple content items (of type `person`) which reference the images. | ||
|
||
In order for the test file to not become overly large, it's been split it into smaller parts: | ||
|
||
=== Mock Portal Library | ||
In addition, mock-xp's `Log` is used to get nice colorful logging. In order for `libPortal.getContent()` to work, `request` property is set on the `libPortal` object. | ||
|
||
TODO: What does this do? | ||
For the test file to not become overly large, it's been split it into smaller parts: | ||
|
||
.src/jest/server/mockXP.ts | ||
[source, typescript] | ||
---- | ||
include::{sourcedir}src/jest/server/mockXP.ts[] | ||
---- | ||
|
||
=== The test | ||
.src/jest/server/preview.test.ts | ||
[source, typescript] | ||
---- | ||
include::{sourcedir}src/jest/server/preview.test.ts[] | ||
---- | ||
|
||
In this test `mock-xp` is used to set up a project repo, a person folder, a couple of images, and a couple content items (of type `person`) which reference the images. | ||
== Output | ||
|
||
In addition, mock-xp's `Log` is used to get nice colorful logging. | ||
Running the test produces the glorious result: | ||
|
||
In order for `libPortal.getContent()` to work, `request` property is set on the `libPortal` object. | ||
npm test src/jest/server/preview.test.ts | ||
|
||
.src/jest/server/preview.test.ts | ||
[source, typescript] | ||
[source, Terminal] | ||
---- | ||
include::{sourcedir}src/jest/server/preview.test.ts[] | ||
> [email protected] test | ||
> jest --no-cache --coverage src/jest/server/preview.test.ts | ||
PASS SERVER src/jest/server/preview.test.ts | ||
preview | ||
✓ is able to render a page for Léa Seydoux (26 ms) | ||
✓ is able to render a page for Jeffrey Wright | ||
----------------------------|---------|----------|---------|---------|------------------- | ||
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s | ||
----------------------------|---------|----------|---------|---------|------------------- | ||
All files | 100 | 54.54 | 100 | 100 | | ||
jest/server | 100 | 100 | 100 | 100 | | ||
mockXP.ts | 100 | 100 | 100 | 100 | | ||
main/resources/controllers | 100 | 16.66 | 100 | 100 | | ||
preview.ts | 100 | 16.66 | 100 | 100 | 16-34 | ||
----------------------------|---------|----------|---------|---------|------------------- | ||
Test Suites: 1 passed, 1 total | ||
Tests: 2 passed, 2 total | ||
Snapshots: 0 total | ||
Time: 1.839 s | ||
Ran all test suites matching /src\/jest\/server\/preview.test.ts/i. | ||
---- | ||
|
||
|
||
== Summary | ||
|
||
This guide, showed how to write tests for Enonic XP applications using Jest in combination with Mock XP. | ||
This chapter demonstrated how to write tests using Jest in combination with Mock XP. | ||
|
||
Mock XP was used to mock the Enonic XP *portal* API. | ||
Mock XP was used to mock the Enonic https://developer.enonic.com/docs/xp/stable/api/lib-content[content^] and https://developer.enonic.com/docs/xp/stable/api/lib-portal[portal^] APIs. | ||
|
||
The next tutorial will show how to write a link:client-side[client-side] test for an Enonic XP application. | ||
Coming up, we demonstrate how to write a link:client-side[client-side] test. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters