Skip to content

Commit

Permalink
Merge branch 'main' into register-context-construct-time
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkucharczyk committed Oct 14, 2024
2 parents 4fe264e + a887d9b commit 008bb94
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-cars-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ember-provide-consume-context': minor
---

Make `consume` and `ContextConsumer` return `undefined` rather than `null`
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
## Building the addon

- `cd ember-provide-consume-context`
- `npm build`
- `npm run build`

## Running tests

Expand Down
4 changes: 2 additions & 2 deletions ember-provide-consume-context/src/-private/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export function consume<K extends keyof ContextRegistry>(
): PropertyDecorator {
return function decorator() {
return {
get(): ContextRegistry[K] | null | undefined {
get(): ContextRegistry[K] | undefined {
if (hasContext(this, contextKey)) {
return getContextValue(this, contextKey);
}

return null;
return undefined;
},
};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ interface ContextConsumerSignature<K extends keyof ContextRegistry> {
defaultValue?: ContextRegistry[K];
};
Blocks: {
default: [ContextRegistry[K] | null];
default: [ContextRegistry[K] | undefined];
};
}

export default class ContextConsumer<
K extends keyof ContextRegistry,
> extends Component<ContextConsumerSignature<K>> {
get contextValue(): ContextRegistry[K] | null {
get contextValue(): ContextRegistry[K] | undefined {
if (hasContext(this, this.args.key)) {
return getContextValue(this, this.args.key);
}

return this.args.defaultValue ?? null;
return this.args.defaultValue;
}
}
15 changes: 15 additions & 0 deletions test-app/app/helpers/eq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { helper } from '@ember/component/helper';

function eq([a, b]: [unknown, unknown]): boolean {
return a === b;
}

const eqHelper = helper(eq);

export default eqHelper;

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
eq: typeof eqHelper;
}
}
10 changes: 10 additions & 0 deletions test-app/tests/integration/components/built-in-components-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ module('Integration | Built-in components', function (hooks) {
this.testContext = testContext;
});

test('consuming a non-existent context returns `undefined`', async function (assert) {
await render(hbs`
<ContextConsumer @key="my-test-context" as |count|>
<div id="content">{{if (eq count undefined) "undefined" "other"}}</div>
</ContextConsumer>
`);

assert.dom('#content').hasText('undefined');
});

test('a consumer can read context', async function (assert) {
await render(hbs`
<ContextProvider @key="my-test-context" @value="5">
Expand Down
26 changes: 26 additions & 0 deletions test-app/tests/integration/components/decorators-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,32 @@ import { tracked } from '@glimmer/tracking';
module('Integration | Decorators', function (hooks) {
setupRenderingTest(hooks);

test('consuming a non-existent context returns `undefined`', async function (assert) {
class TestConsumerComponent extends Component<{
Element: HTMLDivElement;
}> {
@consume('my-test-context') contextValue: string | undefined;
}

setComponentTemplate(
// @ts-ignore
hbs`{{! @glint-ignore }}
<div id="content">{{if (eq this.contextValue undefined) "undefined" "other"}}</div>
`,
TestConsumerComponent,
);

interface TestContext {
TestConsumerComponent: typeof TestConsumerComponent;
}
(this as unknown as TestContext).TestConsumerComponent =
TestConsumerComponent;

await render<TestContext>(hbs`<this.TestConsumerComponent />`);

assert.dom('#content').hasText('undefined');
});

test('a consumer can read context', async function (assert) {
class TestProviderComponent extends Component<{
Element: HTMLDivElement;
Expand Down

0 comments on commit 008bb94

Please sign in to comment.