-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update documentation and add example snapshot tests. Rearrange files …
…to have a snapshot_test directory that exports a DashSnapshotTestCase
- Loading branch information
Showing
12 changed files
with
190 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
end_of_line = lf | ||
charset = utf-8 | ||
|
||
[*.py] | ||
max_line_length = 100 |
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 |
---|---|---|
|
@@ -13,3 +13,6 @@ pyvenv.cfg | |
.venv | ||
pip-selfcheck.json | ||
|
||
.idea/ | ||
*.egg-info/ | ||
*.whl |
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,11 +1,88 @@ | ||
# dash-snapshot-testing | ||
Use snapshot testing to test Dash components | ||
Use snapshot testing, inspired by Jest snapshot testing, to test [Dash][] components. | ||
|
||
## Inspiration | ||
Testing a long HTML component output for a Dash application is difficult. | ||
It typically requires hardcoding data or setting up a dummy database. | ||
Using snapshot tests that JSON serialize the Dash component output provide another | ||
easy testing layer to ensure that code refactors/changes do not change the | ||
output unexpectedly. | ||
|
||
## Example | ||
To learn more about snapshot testing in general, see a much more elaborate explanation from the [Facebook Jest site](https://facebook.github.io/jest/docs/en/snapshot-testing.html) | ||
|
||
## Usage | ||
```python | ||
import dash_html_components as html | ||
|
||
from snapshot_test import DashSnapshotTestCase | ||
|
||
|
||
class MyUnitTestCase(DashSnapshotTestCase): | ||
def test_component(self): | ||
my_component = html.Div([html.P('wow'), html.Span('this works')], id='test-id') | ||
|
||
self.assertSnapshotEqual(my_component, 'my-test-unique-id') | ||
``` | ||
|
||
This outputs/checks this JSON at `__snapshots__/MyUnitTestCase-my-test-unique-id.json`: | ||
```json | ||
{ | ||
"type": "Div", | ||
"props": { | ||
"id": "test-id", | ||
"children": [ | ||
{ | ||
"type": "P", | ||
"props": {"children": "wow"}, | ||
"namespace": "dash_html_components" | ||
}, | ||
{ | ||
"type": "Span", | ||
"props": {"children": "this works"}, | ||
"namespace": "dash_html_components" | ||
} | ||
] | ||
}, | ||
"namespace": "dash_html_components" | ||
} | ||
``` | ||
|
||
### Setting a custom `snapshots_dir` for the class | ||
```python | ||
class MyComponentNameTest(DashSnapshotUnitTest): | ||
def test_func(self) -> None: | ||
class MyOtherUnitTestCase(DashSnapshotTestCase): | ||
snapshots_dir = '__snapshots_2__' | ||
|
||
def test_component(self): | ||
my_component = html.Div([html.P('wow'), html.Span('another one')], id='test-id') | ||
|
||
self.assertSnapshotEqual(my_component, 'my-test-unique-id') | ||
``` | ||
|
||
This outputs/checks this JSON at `__snapshots_2__/MyOtherUnitTestCase-my-test-unique-id.json`: | ||
```json | ||
{ | ||
"type": "Div", | ||
"props": { | ||
"id": "test-id", | ||
"children": [ | ||
{ | ||
"type": "P", | ||
"props": {"children": "wow"}, | ||
"namespace": "dash_html_components" | ||
}, | ||
{ | ||
"type": "Span", | ||
"props": {"children": "another one"}, | ||
"namespace": "dash_html_components" | ||
} | ||
] | ||
}, | ||
"namespace": "dash_html_components" | ||
} | ||
``` | ||
|
||
At its core, this `unittest.TestCase` compares a JSON-serialized Dash component | ||
against a previously stored JSON-serialized Dash component, and checks if the `dict` | ||
objects from `json.loads` are equivalent using `assertEqual`. | ||
|
||
self.assertSnapshotEqual() | ||
``` | ||
[Dash]: https://github.com/plotly/dash |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type": "Div", "props": {"id": "test-id", "children": [{"type": "P", "props": {"children": "wow"}, "namespace": "dash_html_components"}, {"type": "Span", "props": {"children": "another one"}, "namespace": "dash_html_components"}]}, "namespace": "dash_html_components"} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type": "Div", "props": {"id": "test-id", "children": [{"type": "P", "props": {"children": "wow"}, "namespace": "dash_html_components"}, {"type": "Span", "props": {"children": "this works"}, "namespace": "dash_html_components"}]}, "namespace": "dash_html_components"} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-r ./requirements.txt | ||
|
||
dash-html-components | ||
dash-renderer |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dash>=0.19 | ||
plotly>=2.2.3 |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
""" | ||
StratoDem Analytics : __init__.py | ||
Principal Author(s) : Michael Clawar | ||
Secondary Author(s) : | ||
Description : | ||
Notes : | ||
March 27, 2018 | ||
""" | ||
|
||
from .snapshot_test_case import * |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
""" | ||
StratoDem Analytics : __init__.py | ||
Principal Author(s) : Michael Clawar | ||
Secondary Author(s) : | ||
Description : | ||
Notes : | ||
March 27, 2018 | ||
""" |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
StratoDem Analytics : __test_snapshot_test_case | ||
Principal Author(s) : Michael Clawar | ||
Secondary Author(s) : | ||
Description : | ||
Notes : | ||
March 27, 2018 | ||
""" | ||
|
||
|
||
import dash_html_components as html | ||
|
||
from snapshot_test import DashSnapshotTestCase | ||
|
||
|
||
class MyUnitTestCase(DashSnapshotTestCase): | ||
def test_component(self): | ||
my_component = html.Div([html.P('wow'), html.Span('this works')], id='test-id') | ||
|
||
self.assertSnapshotEqual(my_component, 'my-test-unique-id') | ||
|
||
|
||
class MyOtherUnitTestCase(DashSnapshotTestCase): | ||
snapshots_dir = '__snapshots_2__' | ||
|
||
def test_component(self): | ||
my_component = html.Div([html.P('wow'), html.Span('another one')], id='test-id') | ||
|
||
self.assertSnapshotEqual(my_component, 'my-test-unique-id') |
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