-
Notifications
You must be signed in to change notification settings - Fork 265
Translate Test Renderer #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,20 +6,21 @@ layout: docs | |||||||||
category: Reference | ||||||||||
--- | ||||||||||
|
||||||||||
**Importing** | ||||||||||
**Cara Import** | ||||||||||
|
||||||||||
```javascript | ||||||||||
import TestRenderer from 'react-test-renderer'; // ES6 | ||||||||||
const TestRenderer = require('react-test-renderer'); // ES5 with npm | ||||||||||
const TestRenderer = require('react-test-renderer'); // ES5 dengan npm | ||||||||||
``` | ||||||||||
|
||||||||||
## Overview {#overview} | ||||||||||
## Ikhtisar {#overview} | ||||||||||
|
||||||||||
This package provides a React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. | ||||||||||
|
||||||||||
Essentially, this package makes it easy to grab a snapshot of the platform view hierarchy (similar to a DOM tree) rendered by a React DOM or React Native component without using a browser or [jsdom](https://github.com/tmpvar/jsdom). | ||||||||||
*Package* ini menyediakan *renderer* React yang dapat digunakan untuk me-*render* komponen React menjadi objek JavaScript murni, tanpa bergantung pada DOM atau bahasa *native* pada telepon seluler. | ||||||||||
|
||||||||||
Example: | ||||||||||
Pada dasarnya, *package* ini memberi kemudahan untuk mengambil *snapshot* dari hierarki tampilan *platform* (mirip dengan pohon DOM) yang di-*render* oleh React DOM atau komponen React Native tanpa menggunakan *browser* atau [jsdom](https://github.com/tmpvar/jsdom). | ||||||||||
|
||||||||||
Contoh: | ||||||||||
|
||||||||||
```javascript | ||||||||||
import TestRenderer from 'react-test-renderer'; | ||||||||||
|
@@ -38,9 +39,9 @@ console.log(testRenderer.toJSON()); | |||||||||
// children: [ 'Facebook' ] } | ||||||||||
``` | ||||||||||
|
||||||||||
You can use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: [Learn more about it](https://jestjs.io/docs/en/snapshot-testing). | ||||||||||
Anda dapat menggunakan fitur *snapshot testing* dari Jest untuk menyimpan salinan pohon JSON ke *file* secara otomatis dan melakukan pengecekan di tes anda terhadap adanya perubahan (struktur): [Pelajari lebih lanjut](http://facebook.github.io/jest/blog/2016/07/27/jest-14.html). | ||||||||||
|
||||||||||
You can also traverse the output to find specific nodes and make assertions about them. | ||||||||||
Anda juga dapat melintasi ke bagian keluaran untuk menemukan *node* tertentu dan membuat perbandingan terhadap *node-node* tersebut. | ||||||||||
|
||||||||||
```javascript | ||||||||||
import TestRenderer from 'react-test-renderer'; | ||||||||||
|
@@ -95,43 +96,43 @@ expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']); | |||||||||
* [`testInstance.parent`](#testinstanceparent) | ||||||||||
* [`testInstance.children`](#testinstancechildren) | ||||||||||
|
||||||||||
## Reference {#reference} | ||||||||||
## Referensi {#reference} | ||||||||||
|
||||||||||
### `TestRenderer.create()` {#testrenderercreate} | ||||||||||
|
||||||||||
```javascript | ||||||||||
TestRenderer.create(element, options); | ||||||||||
``` | ||||||||||
|
||||||||||
Create a `TestRenderer` instance with the passed React element. It doesn't use the real DOM, but it still fully renders the component tree into memory so you can make assertions about it. Returns a [TestRenderer instance](#testrenderer-instance). | ||||||||||
Membuat *instance* `TestRenderer` dengan mengoper elemen React. Fungsi ini tidak menggunakan DOM asli, tetapi masih sepenuhnya me-*render* diagram komponen ke dalam memori sehingga Anda masih dapat membandingkannya. Mengembalikan sebuah [*instance* TestRenderer](#testrenderer-instance). | ||||||||||
|
||||||||||
### `TestRenderer.act()` {#testrendereract} | ||||||||||
|
||||||||||
```javascript | ||||||||||
TestRenderer.act(callback); | ||||||||||
``` | ||||||||||
|
||||||||||
Similar to the [`act()` helper from `react-dom/test-utils`](/docs/test-utils.html#act), `TestRenderer.act` prepares a component for assertions. Use this version of `act()` to wrap calls to `TestRenderer.create` and `testRenderer.update`. | ||||||||||
Mirip seperti [fungsi pembantu `act()` dari `react-dom/test-utils`](/docs/test-utils.html#act), `TestRenderer.act` menyiapkan komponen untuk dibandingkan. Gunakan versi `act()` ini untuk membungkus panggilan ke `TestRenderer.create` dan `testRenderer.update`. | ||||||||||
|
||||||||||
```javascript | ||||||||||
import {create, act} from 'react-test-renderer'; | ||||||||||
import App from './app.js'; // The component being tested | ||||||||||
import App from './app.js'; // Komponen yang akan dites | ||||||||||
|
||||||||||
// render the component | ||||||||||
// merender komponen | ||||||||||
let root; | ||||||||||
act(() => { | ||||||||||
root = create(<App value={1}/>) | ||||||||||
}); | ||||||||||
|
||||||||||
// make assertions on root | ||||||||||
// membuat perbandingan dengan root | ||||||||||
expect(root.toJSON()).toMatchSnapshot(); | ||||||||||
|
||||||||||
// update with some different props | ||||||||||
// memperbarui dengan prop berbeda | ||||||||||
act(() => { | ||||||||||
root = root.update(<App value={2}/>); | ||||||||||
}) | ||||||||||
|
||||||||||
// make assertions on root | ||||||||||
// membuat perbandingan dengan root | ||||||||||
expect(root.toJSON()).toMatchSnapshot(); | ||||||||||
``` | ||||||||||
|
||||||||||
|
@@ -141,141 +142,141 @@ expect(root.toJSON()).toMatchSnapshot(); | |||||||||
testRenderer.toJSON() | ||||||||||
``` | ||||||||||
|
||||||||||
Return an object representing the rendered tree. This tree only contains the platform-specific nodes like `<div>` or `<View>` and their props, but doesn't contain any user-written components. This is handy for [snapshot testing](https://facebook.github.io/jest/docs/en/snapshot-testing.html#snapshot-testing-with-jest). | ||||||||||
Menghasilkan objek yang merepresentasikan pohon yang telah di-*render*. Pohon ini hanya berisi *node* *platform* khusus seperti `<div>` atau `<View>` dan beserta *props*-nya, tetapi tidak mengandung komponen yang dibuat oleh pengguna. Hal ini berguna untuk [pengujian potret](http://facebook.github.io/jest/docs/en/snapshot-testing.html#snapshot-testing-with-jest). | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
### `testRenderer.toTree()` {#testrenderertotree} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testRenderer.toTree() | ||||||||||
``` | ||||||||||
|
||||||||||
Return an object representing the rendered tree. The representation is more detailed than the one provided by `toJSON()`, and includes the user-written components. You probably don't need this method unless you're writing your own assertion library on top of the test renderer. | ||||||||||
Menghasilkan objek yang merepresentasikan diageam yang telah di-*render*. Tidak seperti `toJSON ()`, representasinya lebih detail daripada yang dihasilkan oleh `toJSON ()`, dan termasuk komponen yang dibuat pengguna. Anda mungkin tidak memerlukan *method* ini kecuali Anda sedang membuat *library* pernyataan sendiri di atas uji *renderer*. | ||||||||||
|
||||||||||
### `testRenderer.update()` {#testrendererupdate} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testRenderer.update(element) | ||||||||||
``` | ||||||||||
|
||||||||||
Re-render the in-memory tree with a new root element. This simulates a React update at the root. If the new element has the same type and key as the previous element, the tree will be updated; otherwise, it will re-mount a new tree. | ||||||||||
Me-*render* ulang pohon dalam memori dengan elemen *root* baru. Hal Ini mensimulasikan pembaruan React di *root*. Jika elemen baru memiliki tipe dan *key* yang sama dengan elemen sebelumnya, struktur pohon akan diperbarui; jika tidak, akan memasang ulang pohon yang baru. | ||||||||||
|
||||||||||
### `testRenderer.unmount()` {#testrendererunmount} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testRenderer.unmount() | ||||||||||
``` | ||||||||||
|
||||||||||
Unmount the in-memory tree, triggering the appropriate lifecycle events. | ||||||||||
Melepas pohon dalam memori, memicu siklus hidup *event* yang sesuai. | ||||||||||
|
||||||||||
### `testRenderer.getInstance()` {#testrenderergetinstance} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testRenderer.getInstance() | ||||||||||
``` | ||||||||||
|
||||||||||
Return the instance corresponding to the root element, if available. This will not work if the root element is a function component because they don't have instances. | ||||||||||
Menghasilkan *instance* yang sesuai dengan elemen *root*, jika tersedia. Hal ini tidak akan berfungsi jika elemen *root* adalah *function component*, karena *function component* tidak memiliki *instance*. | ||||||||||
|
||||||||||
### `testRenderer.root` {#testrendererroot} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testRenderer.root | ||||||||||
``` | ||||||||||
|
||||||||||
Returns the root "test instance" object that is useful for making assertions about specific nodes in the tree. You can use it to find other "test instances" deeper below. | ||||||||||
Menghasilkan objek *root* "*test instance*" yang berguna untuk membuat pernyataan tentang *node* tertentu di pohon. Anda dapat menggunakannya untuk menemukan "*test instance*" lainnya secara lebih dalam di bawah ini. | ||||||||||
|
||||||||||
### `testInstance.find()` {#testinstancefind} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.find(test) | ||||||||||
``` | ||||||||||
|
||||||||||
Find a single descendant test instance for which `test(testInstance)` returns `true`. If `test(testInstance)` does not return `true` for exactly one test instance, it will throw an error. | ||||||||||
Menemukan turunan tunggal *test instance* yang mana `test(testInstance)` menghasilkan nilai `true`. Jika `test(testInstance)` tidak menghasilkan nilai `true` untuk satu *test instance*, fungsi ini akan melemparkan *error*. | ||||||||||
|
||||||||||
### `testInstance.findByType()` {#testinstancefindbytype} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.findByType(type) | ||||||||||
``` | ||||||||||
|
||||||||||
Find a single descendant test instance with the provided `type`. If there is not exactly one test instance with the provided `type`, it will throw an error. | ||||||||||
Menemukan turunan tunggal dari *test instance* berdasarkan `type` yang disediakan. Jika tidak ada satupun *test instance* dengan `type` yang disediakan, fungsi ini akan melemparkan *error*. | ||||||||||
|
||||||||||
### `testInstance.findByProps()` {#testinstancefindbyprops} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.findByProps(props) | ||||||||||
``` | ||||||||||
|
||||||||||
Find a single descendant test instance with the provided `props`. If there is not exactly one test instance with the provided `props`, it will throw an error. | ||||||||||
Menemukan turunan tunggal *test instance* dengan berdasarkan `props` yang disediakan. Jika tidak ada satupun *test instance* dengan `props` yang disediakan, hal ini akan melemparkan *error*. | ||||||||||
|
||||||||||
### `testInstance.findAll()` {#testinstancefindall} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.findAll(test) | ||||||||||
``` | ||||||||||
|
||||||||||
Find all descendant test instances for which `test(testInstance)` returns `true`. | ||||||||||
Menemukan semua turunan *test instance* yang mana `test(testInstance)` menghasilkan nilai `true`. | ||||||||||
|
||||||||||
### `testInstance.findAllByType()` {#testinstancefindallbytype} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.findAllByType(type) | ||||||||||
``` | ||||||||||
|
||||||||||
Find all descendant test instances with the provided `type`. | ||||||||||
Menemukan semua turunan *test instance* dengan berdasarkan `type` yang disediakan. | ||||||||||
|
||||||||||
### `testInstance.findAllByProps()` {#testinstancefindallbyprops} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.findAllByProps(props) | ||||||||||
``` | ||||||||||
|
||||||||||
Find all descendant test instances with the provided `props`. | ||||||||||
Menemukan semua turunan *test instance* dengan berdasarkan `props` yang disediakan. | ||||||||||
|
||||||||||
### `testInstance.instance` {#testinstanceinstance} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.instance | ||||||||||
``` | ||||||||||
|
||||||||||
The component instance corresponding to this test instance. It is only available for class components, as function components don't have instances. It matches the `this` value inside the given component. | ||||||||||
*Instance* dari komponen yang ada di *test instance*. Hanya tersedia untuk *class component*, karena *function component* tidak memiliki *instance*. Hal Ini sama dengan nilai `this` di dalam komponen yang diberikan. | ||||||||||
|
||||||||||
### `testInstance.type` {#testinstancetype} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.type | ||||||||||
``` | ||||||||||
|
||||||||||
The component type corresponding to this test instance. For example, a `<Button />` component has a type of `Button`. | ||||||||||
Jenis dari komponen yang ada di *test instance*. Sebagai contoh, komponen `<Button />` memiliki tipe `Button`. | ||||||||||
|
||||||||||
### `testInstance.props` {#testinstanceprops} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.props | ||||||||||
``` | ||||||||||
|
||||||||||
The props corresponding to this test instance. For example, a `<Button size="small" />` component has `{size: 'small'}` as props. | ||||||||||
*Props* yang ada di *test instance*. Sebagai contoh, komponen `<Button size="small" />` memiliki `{size: 'small'}` sebagai *props*. | ||||||||||
|
||||||||||
### `testInstance.parent` {#testinstanceparent} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.parent | ||||||||||
``` | ||||||||||
|
||||||||||
The parent test instance of this test instance. | ||||||||||
Induk *test instance* dari *test instance* ini. | ||||||||||
|
||||||||||
### `testInstance.children` {#testinstancechildren} | ||||||||||
|
||||||||||
```javascript | ||||||||||
testInstance.children | ||||||||||
``` | ||||||||||
|
||||||||||
The children test instances of this test instance. | ||||||||||
Anak *test instance* dari *test instance* ini. | ||||||||||
|
||||||||||
## Ideas {#ideas} | ||||||||||
## Gagasan {#ideas} | ||||||||||
|
||||||||||
You can pass `createNodeMock` function to `TestRenderer.create` as the option, which allows for custom mock refs. | ||||||||||
`createNodeMock` accepts the current element and should return a mock ref object. | ||||||||||
This is useful when you test a component that relies on refs. | ||||||||||
Anda dapat mengoper *function* `createNodeMock` ke ` TestRenderer.create` sebagai opsinya, yang memungkinkan untuk referensi tiruan yang sudah diubah. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
`createNodeMock` menerima elemen tersebut dan harus menghasilkan objek referensi tiruan. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
Hal Ini berguna ketika Anda menguji sebuah komponen yang bergantung pada referensi. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
|
||||||||||
```javascript | ||||||||||
import TestRenderer from 'react-test-renderer'; | ||||||||||
|
@@ -299,7 +300,7 @@ TestRenderer.create( | |||||||||
{ | ||||||||||
createNodeMock: (element) => { | ||||||||||
if (element.type === 'input') { | ||||||||||
// mock a focus function | ||||||||||
// tiruan function focus | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tiruan dari fungsi focus There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
return { | ||||||||||
focus: () => { | ||||||||||
focused = true; | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello -> Halo to make it consistent with other pages 😄