Guide
- Installation
- Introduction
- Getting Started
- Declarative Rendering
- Basic Components
- Conditionals and Loops
- Composing with components
- Handling user input
- Animations
- Slots
- Component As Prop
- Device APIs
- React Native
- Ready for more?
- Vue Native Router
- Testing
- Community Libraries
- How does it work?
- How to contribute
- FAQ
Testing
Snapshot testing
Snapshot tests are a very useful tool whenever you want to make sure your UI does not change unexpectedly.A typical snapshot test case for a mobile app renders a UI component, takes a screenshot, then compares it to a reference image stored alongside the test. The test will fail if the two images do not match.
A similar approach can be taken when it comes to testing your Vue-Native components. Instead of rendering the graphical UI, which would require building the entire app,you can use a test renderer to quickly generate a serializable value.
Since we want to parse and take a screenshot of *.vue
files we first read these files and parse them.
Follow the below snippets and add them to the file *-test.js
under the __tests__
folder:
• Read the vue file content using node’s fs
package.
var fs = require("fs"); |
• Parse the data
using vue-native-scripts
‘ reactVueTemplateParser
function to extract elements from the vue code.
const Login = reactVueTemplateParser(data); |
Now use the parsed data to render using the renderer
from react-test-renderer
.
describe("Login Component", () => { |
Run npm test -- -u
which runs the test and creates a snapshot
and replaces anything in the *-test.js.snap
file under the __snapshots__
folder.
The snapshot created looks something like this :
// Jest Snapshot v1, https://goo.gl/fbAQLP |
The vue file used here looks like this :
<template> |
<style> |
Changing anything in the vue file would result in a failed test (npm test
) since it compares the current image with the snapshot it already has.
Running npm test -- -u
would replace the snapshot with the updated UI components.