-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add context provider test helpers (#20)
- Loading branch information
1 parent
b7e57d2
commit 611b1df
Showing
9 changed files
with
303 additions
and
15 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,5 @@ | ||
--- | ||
"ember-provide-consume-context": patch | ||
--- | ||
|
||
Add context provider test helpers |
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
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
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,57 @@ | ||
import { getContext } from '@ember/test-helpers'; | ||
import type { TestContext } from '@ember/test-helpers'; | ||
import type { ProvideConsumeContextContainer } from '../-private/provide-consume-context-container'; | ||
import type ContextRegistry from '../context-registry'; | ||
|
||
export function setupRenderWrapper(templateFactory: object) { | ||
const context = getContext() as TestContext | undefined; | ||
if (context == null) { | ||
throw new Error('Could not find test context'); | ||
} | ||
|
||
if (context.owner == null) { | ||
throw new Error('Could not find owner on test context'); | ||
} | ||
|
||
const { owner } = context; | ||
|
||
// Registers a custom outlet to use in the test, similar to how test-helpers does it: | ||
// https://github.com/emberjs/ember-test-helpers/blob/9cec68dc6aa9c0a7a449eb89797eb81299fa727f/addon/addon-test-support/%40ember/test-helpers/setup-rendering-context.ts#L68 | ||
// Casting "as any" because "unregister" isn't defined on the Owner type, but it does exist. | ||
(owner as any).unregister('template:-outlet'); | ||
owner.register('template:-outlet', templateFactory); | ||
} | ||
|
||
export function provide< | ||
T extends keyof ContextRegistry, | ||
U extends ContextRegistry[T], | ||
>(name: T, value: U) { | ||
const context = getContext() as TestContext | undefined; | ||
if (context?.owner != null) { | ||
const { owner } = context; | ||
|
||
// https://github.com/emberjs/ember.js/blob/57073a0e9751d036d4bcfc11d5367e3f6ae751d2/packages/%40ember/-internals/glimmer/lib/renderer.ts#L284 | ||
// We cast to "any", because Renderer is a private API and isn't easily accessible. | ||
// Even if we imported the type, "_runtime" is marked as private, | ||
// so we wouldn't be able to access the current runtime or its type. | ||
// If Context was implemented in Ember proper, it would have access to those private | ||
// APIs, and this wouldn't look quite as illegal anymore. | ||
const renderer = owner.lookup('renderer:-dom') as any; | ||
|
||
if (renderer == null) { | ||
throw new Error('Could not find "renderer:-dom" on owner'); | ||
} | ||
|
||
const container = renderer._runtime?.env?.provideConsumeContextContainer as | ||
| ProvideConsumeContextContainer | ||
| undefined; | ||
|
||
if (container == null) { | ||
throw new Error( | ||
'Could not find "provideConsumeContextContainer" instance in runtime environment', | ||
); | ||
} | ||
|
||
container.registerMockProvider(name, value); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.