{"id":27817068,"url":"https://github.com/devgalena/svelte-context-test","last_synced_at":"2025-05-01T14:34:03.581Z","repository":{"id":57711184,"uuid":"515180519","full_name":"devgalena/svelte-context-test","owner":"devgalena","description":null,"archived":false,"fork":false,"pushed_at":"2022-07-19T05:30:37.000Z","size":82,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-28T06:09:53.986Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/devgalena.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-18T12:44:36.000Z","updated_at":"2025-01-05T09:40:05.000Z","dependencies_parsed_at":"2022-09-26T21:30:31.445Z","dependency_job_id":null,"html_url":"https://github.com/devgalena/svelte-context-test","commit_stats":null,"previous_names":["devgalena/svelte-context-test","krestui/svelte-context-test"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgalena%2Fsvelte-context-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgalena%2Fsvelte-context-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgalena%2Fsvelte-context-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devgalena%2Fsvelte-context-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devgalena","download_url":"https://codeload.github.com/devgalena/svelte-context-test/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251890104,"owners_count":21660448,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-05-01T14:31:32.924Z","updated_at":"2025-05-01T14:34:03.572Z","avatar_url":"https://github.com/devgalena.png","language":"JavaScript","readme":"# Svelte Context Test\n\n`@krestui/svelte-context-test` is a library to test components with svelte context, specifically `getContext()` API-s.\n\nsvelte framework provides and recommends `Context`s to share data between the parent and child components, .\n\nThis library helps to test Svelte Child Components that expect context values to be passed from a parent component.\n\n## Installation\n\n```sh\n$ npm install -D @krestui/svelte-context-test\n```\n\nIn case of yarn, do the following:\n\n```sh\n$ yarn add -D @krestui/svelte-context-test\n```\n\n## Example\n\nAssume 2 components - Parent and Child, that share data between each other through `Context`s , as below.\n\n`Child.svelte`\n\n```svelte\n\u003cscript lang=\"ts\"\u003e\n  import { getContext } from 'svelte';\n  import type { Writable } from 'svelte/store';\n\n  const groupStore: Writable\u003cstring\u003e = getContext('ctx-key');\n\u003c/script\u003e\n\n{$groupStore}\n```\n\nThe Child component expects context data through a key - `ctx-key` as mentioned above.\n\n`Parent.svelte`\n\n```svelte\n\u003cscript lang=\"ts\"\u003e\n  import { setContext } from 'svelte';\n  import { writable } from 'svelte/store';\n  import Child from './Child.svelte';\n\n  let groupStore = writable('');\n\n  setContext('ctx-key', groupStore);\n\u003c/script\u003e\n\n\u003cChild /\u003e\n```\n\nThe Parent component sets the `ctx-key` context key so the child component can pick it up.\n\nTo test the child component `Child.svelte` above (using jest or any other framework of your choice),we need to set the right context value for `ctx-key` .\n\nTrying to instantiate the child component directly through the `render` from `testing-library/svelte` will work, but not very useful since we cannot set the value of the context key `ctx-key` expected in the `Child` component.\n\n`Child-notuseful.test.ts`\n\n```typescript\nimport { render, fireEvent } from '@testing-library/svelte';\n\nimport Child from './Child.svelte';\n\ntest('renders the Child component', () =\u003e {\n  const { getByText } = render(Child);\n  // This will programmatically compile but useless\n  // since we cannot set the context key of 'ctx-key' to be tested better\n});\n```\n\nTo write better unit tests, we use this library `@krestui/svelte-context-test` as below.\n\n## Usage\n\nCreate a mixin helper component of `Child.svelte` as below, say `MockChild.svelte` that helps us set the context key attribute values, as below.\n\nUse the `ContextTest` component provided by this library to instantiate the `Child` component while setting the context key attribute values.\n\nSee below.\n\n`MockChild.svelte`\n\n```svelte\n\u003cscript lang=\"ts\"\u003e\n  import Child from '$lib/Child.svelte';\n  import { writable } from 'svelte/store';\n  import ContextTest, { type KeyValue } from '@krestui/svelte-context-test/ContextTest.svelte';\n  const keyValues: Array\u003cKeyValue\u003e = [{ key: 'ctx-key', value: writable('') } as KeyValue];\n\u003c/script\u003e\n\n\u003cContextTest component={Child} {keyValues} /\u003e\n```\n\n`Child.test.ts`\n\n```typescript\nimport '@testing-library/jest-dom';\nimport { render } from '@testing-library/svelte';\nimport MockChild from './MockChild.svelte';\n\ntest('renders Child Component', () =\u003e {\n  const { getByText } = render(MockChild);\n});\n```\n\n### jest\n\nIf you are using `jest` freel free to add the following to `jest.config.cjs` in your project\n\njest.config.cjs\n\n```js\n  transformIgnorePatterns: [\n    '\u003crootDir\u003e/node_modules/(?!(@krestui)/(svelte-context-test))'\n  ],\n```\n\nThe above will ensure that the `jest` runner is able to read `svelte` files from this package so the unit tests continue to pass.\n\n## License\n\nThe code is issued under MIT License.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevgalena%2Fsvelte-context-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevgalena%2Fsvelte-context-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevgalena%2Fsvelte-context-test/lists"}