Skip to content

feat: fix README demo + live demo in CodeSandbox with Tests #44

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

Merged
merged 3 commits into from
Aug 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,21 @@ primary guiding principle is:

## Example

You can check the live example at CodeSandbox, "Browser" tab renders App.svelte and "Tests" tab runs App.spec.js

- **Live demo:** https://codesandbox.io/s/live-demo-svelte-testing-library-q8iv7

App.svelte

```html
<script>
export let name

let buttonText = "Button Text";

function handleClick() {
buttonText = "Button Clicked";
}
</script>

<style>
Expand All @@ -105,30 +115,41 @@ App.svelte
</style>

<h1>Hello {name}!</h1>

<button on:click={handleClick}>{buttonText}</button>
```

App.spec.js

```javascript
import App from '../src/App.svelte'
import {render} from '@testing-library/svelte'
describe('App', () => {
test('should render greeting', () => {
const {getByText} = render(App, {props: {name: 'world'}})
import App from "./App.svelte";
import {
render,
cleanup,
fireEvent,
waitForElement
} from "@testing-library/svelte";
import "@testing-library/jest-dom/extend-expect";

afterEach(cleanup);

describe("App", () => {
test("should render greeting", () => {
const { getByText } = render(App, { props: { name: "world" } });

expect(getByText('Hello world!'))
})
expect(getByText("Hello world!"));
});

test('should change button text after click', async () => {
const {getByText} = render(App, {props: {name: 'world'}})
test("should change button text after click", async () => {
const { getByText } = render(App, { props: { name: "world" } });

fireEvent.click(getByText('Button Text'))
fireEvent.click(getByText("Button Text"));

const button = await waitForElement(() => getByText('Button Clicked'))
const button = await waitForElement(() => getByText("Button Clicked"));

expect(button).toBeInTheDocument()
})
})
expect(button).toBeInTheDocument();
});
});
```

## Installation
Expand Down