Skip to content

Commit

Permalink
Add tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkucharczyk authored Jan 4, 2024
1 parent 5b9f1b4 commit f59c432
Show file tree
Hide file tree
Showing 16 changed files with 4,310 additions and 5,513 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-jars-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ember-provide-consume-context": patch
---

Add tests
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ jobs:
with:
node-version: 18
cache: npm
# https://discord.com/channels/480462759797063690/1049716681225797723/1049738716635205772
- name: Set legacy peer dependencies
run: npm config set legacy-peer-deps=true --location=project
- name: Install Dependencies
run: npm ci
- name: Run Tests
Expand Down
3 changes: 3 additions & 0 deletions ember-provide-consume-context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
"@embroider/addon-shim": "^1.0.0",
"@glimmer/component": "^1.1.2"
},
"peerDependencies": {
"ember-source": "^4.8.0 || ^5.0.0"
},
"devDependencies": {
"@babel/core": "^7.17.0",
"@babel/plugin-proposal-class-properties": "^7.16.7",
Expand Down
15 changes: 14 additions & 1 deletion ember-provide-consume-context/src/-private/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { getOwner } from '@ember/owner';
import type ContextRegistry from '../context-registry';
import {
dependencySatisfies,
importSync,
macroCondition,
} from '@embroider/macros';
import type Owner from '@ember/owner';

let getOwner: (context: unknown) => Owner | undefined;

if (macroCondition(dependencySatisfies('ember-source', '>=4.10.0'))) {
getOwner = (importSync('@ember/owner') as any).getOwner;
} else {
getOwner = (importSync('@ember/application') as any).getOwner;
}

// TODO: See if we can type the owner
export function getProvider(owner: any, contextKey: keyof ContextRegistry) {
Expand Down

This file was deleted.

9,323 changes: 3,863 additions & 5,460 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion test-app/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ module.exports = {
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
],
rules: {},
rules: {
'@typescript-eslint/ban-ts-comment': ['warn'],
},
},
// node files
{
Expand Down
8 changes: 8 additions & 0 deletions test-app/.template-lintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@
module.exports = {
plugins: ['ember-template-lint-plugin-prettier'],
extends: ['recommended', 'ember-template-lint-plugin-prettier:recommended'],
overrides: [
{
files: ['**/integration/**/*-test.{js,ts}'],
rules: {
prettier: false,
},
},
],
};
2 changes: 2 additions & 0 deletions test-app/config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module.exports = async function () {
},
{
name: 'ember-beta',
allowedToFail: true,
npm: {
devDependencies: {
'ember-source': await getChannelURL('beta'),
Expand All @@ -40,6 +41,7 @@ module.exports = async function () {
},
{
name: 'ember-canary',
allowedToFail: true,
npm: {
devDependencies: {
'ember-source': await getChannelURL('canary'),
Expand Down
23 changes: 7 additions & 16 deletions test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"test:ember": "ember test"
},
"devDependencies": {
"ember-provide-consume-context": "^0.1.0",
"ember-provide-consume-context": "*",
"@ember/optional-features": "^2.0.0",
"@ember/string": "^3.0.1",
"@ember/test-helpers": "^2.9.3",
"@ember/test-helpers": "^3.2.1",
"@embroider/test-setup": "^3.0.1",
"@glimmer/component": "^1.1.2",
"@glimmer/tracking": "^1.1.2",
Expand All @@ -57,39 +57,30 @@
"@types/ember__string": "^3.0.11",
"@types/ember__template": "^4.0.2",
"@types/ember__test": "^4.0.2",
"@types/ember__test-helpers": "^2.9.1",
"@types/ember__utils": "^4.0.3",
"@types/ember-data": "^4.4.11",
"@types/ember-data__adapter": "^4.0.2",
"@types/ember-data__model": "^4.0.1",
"@types/ember-data__serializer": "^4.0.2",
"@types/ember-data__store": "^4.0.3",
"@types/ember-qunit": "^6.1.1",
"@types/ember-resolver": "^9.0.0",
"@types/qunit": "^2.19.6",
"@types/rsvp": "^4.0.4",
"@typescript-eslint/eslint-plugin": "^5.57.1",
"@typescript-eslint/parser": "^5.57.1",
"broccoli-asset-rev": "^3.0.0",
"concurrently": "^8.0.1",
"ember-auto-import": "^2.6.3",
"ember-cli": "~4.12.1",
"ember-cli": "~4.12.2",
"ember-cli-app-version": "^6.0.0",
"ember-cli-babel": "^7.26.11",
"ember-cli-dependency-checker": "^3.3.1",
"ember-cli-dependency-checker": "^3.3.2",
"ember-cli-htmlbars": "^6.2.0",
"ember-cli-inject-live-reload": "^2.1.0",
"ember-cli-sri": "^2.1.1",
"ember-cli-terser": "^4.0.2",
"ember-cli-typescript": "^5.2.1",
"ember-data": "~4.11.3",
"ember-fetch": "^8.1.2",
"ember-load-initializers": "^2.1.2",
"ember-modifier": "^4.1.0",
"ember-page-title": "^7.0.0",
"ember-qunit": "^6.2.0",
"ember-qunit": "^8.0.2",
"ember-resolver": "^10.0.0",
"ember-source": "~4.12.0",
"ember-source": "~4.12.2",
"ember-source-channel-url": "^3.0.0",
"ember-template-lint": "^5.7.2",
"ember-template-lint-plugin-prettier": "^5.0.0",
Expand All @@ -112,7 +103,7 @@
"webpack": "^5.88.2"
},
"engines": {
"node": "14.* || 16.* || >= 18"
"node": "16.* || >= 18"
},
"ember": {
"edition": "octane"
Expand Down
216 changes: 216 additions & 0 deletions test-app/tests/integration/components/built-in-components-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, settled } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { tracked } from '@glimmer/tracking';

class TestContext {
@tracked count?: number;
@tracked hidden?: boolean;
}

interface ThisContext {
testContext: TestContext;
}

module('Integration | Built-in components', function (hooks) {
setupRenderingTest(hooks);

let testContext: TestContext;

hooks.beforeEach(function (this: ThisContext) {
testContext = new TestContext();
this.testContext = testContext;
});

test('a consumer can read context', async function (assert) {
await render(hbs`
<ContextProvider @key="my-test-context" @value="5">
<ContextConsumer @key="my-test-context" as |count|>
<div id="content">{{count}}</div>
</ContextConsumer>
</ContextProvider>
`);

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

test('a consumer reads from closest context', async function (assert) {
await render(hbs`
<ContextProvider @key="my-test-context" @value="1">
<ContextConsumer @key="my-test-context" as |count|>
<div id="content-1">{{count}}</div>
</ContextConsumer>
<ContextProvider @key="my-test-context" @value="2">
<ContextConsumer @key="my-test-context" as |count|>
<div id="content-2">{{count}}</div>
</ContextConsumer>
</ContextProvider>
</ContextProvider>
`);

assert.dom('#content-1').hasText('1');
assert.dom('#content-2').hasText('2');
});

test("a consumer's value updates when provider value changes", async function (assert) {
testContext.count = 1;

await render<ThisContext>(hbs`
<ContextProvider @key="my-test-context" @value={{this.testContext.count}}>
<ContextConsumer @key="my-test-context" as |count|>
<div id="content">{{count}}</div>
</ContextConsumer>
</ContextProvider>
`);

assert.dom('#content').hasText('1');

testContext.count = 2;
await settled();
assert.dom('#content').hasText('2');
});

test("a consumer can't access a context it's not nested in", async function (assert) {
await render(hbs`
<ContextProvider @key="my-test-context-1" @value="1">
<ContextConsumer @key="my-test-context-1" as |count|>
<div id="content-1">{{count}}</div>
</ContextConsumer>
</ContextProvider>
<ContextProvider @key="my-test-context-2" @value="2">
<ContextConsumer @key="my-test-context-1" as |count|>
<div id="content-2">{{count}}</div>
</ContextConsumer>
</ContextProvider>
`);

assert.dom('#content-2').hasText('');
});

test('sibling contexts with same key', async function (assert) {
await render(hbs`
<ContextProvider @key="my-test-context" @value="1">
<ContextConsumer @key="my-test-context" as |count|>
<div id="content-1">{{count}}</div>
</ContextConsumer>
</ContextProvider>
<ContextProvider @key="my-test-context" @value="2">
<ContextConsumer @key="my-test-context" as |count|>
<div id="content-2">{{count}}</div>
</ContextConsumer>
</ContextProvider>
`);

assert.dom('#content-1').hasText('1');
assert.dom('#content-2').hasText('2');
});

test('consuming context in conditional', async function (assert) {
testContext.count = 1;
testContext.hidden = false;

await render<ThisContext>(hbs`
<ContextProvider @key="my-test-context" @value={{this.testContext.count}}>
{{#unless this.testContext.hidden}}
<ContextConsumer @key="my-test-context" as |count|>
<div id="content">{{count}}</div>
</ContextConsumer>
{{/unless}}
</ContextProvider>
`);

assert.dom('#content').exists();
assert.dom('#content').hasText('1');

testContext.hidden = true;
await settled();
assert.dom('#content').doesNotExist();

testContext.hidden = false;
await settled();
assert.dom('#content').exists();
assert.dom('#content').hasText('1');

testContext.hidden = true;
await settled();
testContext.count = 2;
await settled();
testContext.hidden = false;
await settled();
assert.dom('#content').exists();
assert.dom('#content').hasText('2');
});

test('context provider in conditional', async function (assert) {
testContext.hidden = false;

await render<ThisContext>(hbs`
{{#unless this.testContext.hidden}}
<ContextProvider @key="my-test-context" @value="1">
<ContextConsumer @key="my-test-context" as |count|>
<div id="content">{{count}}</div>
</ContextConsumer>
</ContextProvider>
{{/unless}}
`);

assert.dom('#content').exists();
assert.dom('#content').hasText('1');

testContext.hidden = true;
await settled();
assert.dom('#content').doesNotExist();

testContext.hidden = false;
await settled();
assert.dom('#content').exists();
assert.dom('#content').hasText('1');
});

test('consuming context with another provider in conditional sibling', async function (assert) {
testContext.hidden = true;

await render<ThisContext>(hbs`
<ContextProvider @key="my-test-context" @value="1">
{{#unless this.testContext.hidden}}
<ContextProvider @key="my-test-context" @value="2">
</ContextProvider>
{{/unless}}
<ContextConsumer @key="my-test-context" as |count|>
<div id="content">{{count}}</div>
</ContextConsumer>
</ContextProvider>
`);

assert.dom('#content').hasText('1');

testContext.hidden = false;
await settled();
assert.dom('#content').hasText('1');

testContext.hidden = true;
await settled();
assert.dom('#content').hasText('1');
});

test('nesting different contexts', async function (assert) {
await render(hbs`
<ContextProvider @key="my-test-context" @value="1">
<ContextProvider @key="my-test-context-2" @value="2">
<ContextConsumer @key="my-test-context" as |contextOne|>
<div id="content-1">{{contextOne}}</div>
</ContextConsumer>
<ContextConsumer @key="my-test-context-2" as |contextTwo|>
<div id="content-2">{{contextTwo}}</div>
</ContextConsumer>
</ContextProvider>
</ContextProvider>
`);

assert.dom('#content-1').hasText('1');
assert.dom('#content-2').hasText('2');
});
});
Loading

0 comments on commit f59c432

Please sign in to comment.