From f92e65a5e45cffaf15c11f24a9c550de01814035 Mon Sep 17 00:00:00 2001 From: Alex LaFroscia Date: Sat, 26 Jan 2019 02:26:06 -0800 Subject: [PATCH] feat: add helper subscribes to observable --- addon/.gitkeep | 0 addon/helpers/subscribe.js | 15 ++ app/.gitkeep | 0 app/helpers/subscribe.js | 1 + package.json | 6 +- tests/integration/helpers/subscribe-test.js | 51 +++++++ tests/test-helper.js | 9 +- yarn.lock | 143 +++++++++++++++++++- 8 files changed, 217 insertions(+), 8 deletions(-) delete mode 100644 addon/.gitkeep create mode 100644 addon/helpers/subscribe.js delete mode 100644 app/.gitkeep create mode 100644 app/helpers/subscribe.js create mode 100644 tests/integration/helpers/subscribe-test.js diff --git a/addon/.gitkeep b/addon/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/addon/helpers/subscribe.js b/addon/helpers/subscribe.js new file mode 100644 index 0000000..7ed8512 --- /dev/null +++ b/addon/helpers/subscribe.js @@ -0,0 +1,15 @@ +import BaseStreamHelper from "ember-stream-helper"; + +class SubscribeHelper extends BaseStreamHelper { + subscribe([observable]) { + const subscription = observable.subscribe(value => { + this.emit(value); + }); + + return () => { + subscription.unsubscribe(); + }; + } +} + +export default SubscribeHelper; diff --git a/app/.gitkeep b/app/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/app/helpers/subscribe.js b/app/helpers/subscribe.js new file mode 100644 index 0000000..beeb0d5 --- /dev/null +++ b/app/helpers/subscribe.js @@ -0,0 +1 @@ +export { default, subscribe } from 'ember-observable/helpers/subscribe'; diff --git a/package.json b/package.json index 9b4e804..941109c 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ "dependencies": { "ember-auto-import": "^1.2.19", "ember-cli-babel": "^7.1.2", + "ember-stream-helper": "^1.0.1", "rxjs": "^6.3.3" }, "devDependencies": { @@ -36,6 +37,8 @@ "ember-cli-inject-live-reload": "^1.8.2", "ember-cli-sri": "^2.1.1", "ember-cli-template-lint": "^1.0.0-beta.1", + "ember-cli-testdouble": "^0.1.4", + "ember-cli-testdouble-qunit": "^2.1.1", "ember-cli-uglify": "^2.1.0", "ember-disable-prototype-extensions": "^1.1.3", "ember-export-application-global": "^2.0.0", @@ -49,7 +52,8 @@ "eslint-plugin-ember": "^5.2.0", "eslint-plugin-node": "^7.0.1", "loader.js": "^4.7.0", - "qunit-dom": "^0.8.0" + "qunit-dom": "^0.8.0", + "testdouble": "^3.0.0" }, "engines": { "node": "6.* || 8.* || >= 10.*" diff --git a/tests/integration/helpers/subscribe-test.js b/tests/integration/helpers/subscribe-test.js new file mode 100644 index 0000000..126f57b --- /dev/null +++ b/tests/integration/helpers/subscribe-test.js @@ -0,0 +1,51 @@ +import { module, test } from "qunit"; +import { setupRenderingTest } from "ember-qunit"; +import { render, clearRender, settled } from "@ember/test-helpers"; +import hbs from "htmlbars-inline-precompile"; +import td from "testdouble"; +import { Observable } from "rxjs"; + +module("Integration | Helper | subscribe", function(hooks) { + setupRenderingTest(hooks); + + hooks.beforeEach(function() { + this.observable = new Observable(observer => { + this.next = value => { + observer.next(value); + + return settled(); + }; + }); + }); + + test("it emits values from the observable", async function(assert) { + await render(hbs` + {{subscribe observable}} + `); + + await this.next("Some Value"); + + assert.dom().hasText("Some Value", "Emits the initial value"); + + await this.next("Next Value"); + + assert.dom().hasText("Next Value", "Emits additional values"); + }); + + test("it unsubscribes when the helper is destroyed", async function(assert) { + const subscribe = td.replace(this.observable, "subscribe"); + const subscription = td.object(); + + td.when(subscribe(td.matchers.isA(Function))).thenReturn(subscription); + + await render(hbs` + {{subscribe observable}} + `); + await clearRender(); + + assert.verify( + subscription.unsubscribe(), + "Unsubscribes from the observable when the helper is destroyed" + ); + }); +}); diff --git a/tests/test-helper.js b/tests/test-helper.js index 0382a84..545be84 100644 --- a/tests/test-helper.js +++ b/tests/test-helper.js @@ -1,7 +1,8 @@ -import Application from '../app'; -import config from '../config/environment'; -import { setApplication } from '@ember/test-helpers'; -import { start } from 'ember-qunit'; +import Application from "../app"; +import config from "../config/environment"; +import { setApplication } from "@ember/test-helpers"; +import { start } from "ember-qunit"; +import "ember-cli-testdouble-qunit"; setApplication(Application.create(config.APP)); diff --git a/yarn.lock b/yarn.lock index 91f5de1..90e23fe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2092,7 +2092,7 @@ broccoli-funnel-reducer@^1.0.0: resolved "https://registry.yarnpkg.com/broccoli-funnel-reducer/-/broccoli-funnel-reducer-1.0.0.tgz#11365b2a785aec9b17972a36df87eef24c5cc0ea" integrity sha1-ETZbKnha7JsXlyo234fu8kxcwOo= -broccoli-funnel@^1.0.0, broccoli-funnel@^1.0.1: +broccoli-funnel@^1.0.0, broccoli-funnel@^1.0.1, broccoli-funnel@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/broccoli-funnel/-/broccoli-funnel-1.2.0.tgz#cddc3afc5ff1685a8023488fff74ce6fb5a51296" integrity sha1-zdw6/F/xaFqAI0iP/3TOb7WlEpY= @@ -3170,6 +3170,13 @@ cyclist@~0.2.2: resolved "https://registry.yarnpkg.com/cyclist/-/cyclist-0.2.2.tgz#1b33792e11e914a2fd6d6ed6447464444e5fa640" integrity sha1-GzN5LhHpFKL9bW7WRHRkRE5fpkA= +d@1: + version "1.0.0" + resolved "https://registry.yarnpkg.com/d/-/d-1.0.0.tgz#754bb5bfe55451da69a58b94d45f4c5b0462d58f" + integrity sha1-dUu1v+VUUdpppYuU1F9MWwRi1Y8= + dependencies: + es5-ext "^0.10.9" + dag-map@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/dag-map/-/dag-map-2.0.2.tgz#9714b472de82a1843de2fba9b6876938cab44c68" @@ -3461,7 +3468,7 @@ ember-cli-babel@^6.0.0-beta.4, ember-cli-babel@^6.0.0-beta.7, ember-cli-babel@^6 ember-cli-version-checker "^2.1.2" semver "^5.5.0" -ember-cli-babel@^7.1.2, ember-cli-babel@^7.1.3: +ember-cli-babel@^7.1.2, ember-cli-babel@^7.1.3, ember-cli-babel@^7.2.0, ember-cli-babel@^7.3.0: version "7.4.0" resolved "https://registry.yarnpkg.com/ember-cli-babel/-/ember-cli-babel-7.4.0.tgz#8fc38a650629c0408adf50a46deef91fcd9d0f1d" integrity sha512-nCEniuWcZMZ12uJopHAMEzj0J55fDEKVEr0adoYZNl7e3E6Pz4WyixyddhhzNa+WcOl9ZULYbH/1R0AL4Tyzjg== @@ -3627,6 +3634,25 @@ ember-cli-test-loader@^2.2.0: dependencies: ember-cli-babel "^6.8.1" +ember-cli-testdouble-qunit@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/ember-cli-testdouble-qunit/-/ember-cli-testdouble-qunit-2.1.1.tgz#29248eaf4d267fe493eaf449328e29a7fb27ab42" + integrity sha512-w2eFraIz25KtUe0gK5NzHByoW0maczBzoB40IEbV8qnHzOwRkf6kvR36MByXglBXUPGf9G2ZfTTKChK7KcOyeg== + dependencies: + broccoli-funnel "^2.0.1" + ember-cli-babel "^7.2.0" + testdouble-qunit "^2.1.1" + +ember-cli-testdouble@^0.1.4: + version "0.1.4" + resolved "https://registry.yarnpkg.com/ember-cli-testdouble/-/ember-cli-testdouble-0.1.4.tgz#fac95c45a1567f70929521051e049091c198292d" + integrity sha512-bzgyduq9X5mCCEXK6KffpmYIaOGis0l9oQACiKiijo8RXLqSuZro0T/f9ifIzoT3J2tfvNPzYBwCY4VzUagBcw== + dependencies: + broccoli-funnel "^1.2.0" + broccoli-merge-trees "^2.0.0" + ember-cli-babel "^6.6.0" + testdouble "^3.5.2" + ember-cli-uglify@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/ember-cli-uglify/-/ember-cli-uglify-2.1.0.tgz#4a0641fe4768d7ab7d4807aca9924cc77c544184" @@ -3833,6 +3859,13 @@ ember-source@~3.7.0: jquery "^3.3.1" resolve "^1.6.0" +ember-stream-helper@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/ember-stream-helper/-/ember-stream-helper-1.0.1.tgz#f9bbb011925ecbbc31d2f2ec32a6aeb8f57b7305" + integrity sha512-zQs3RxRLrpFvW+YG1b4JZ1GsAuUk4wEJNpXRzIWYXM+60YHmESxsGtVBXljuqzwiwjupy+10osGO7L2xCtfgyg== + dependencies: + ember-cli-babel "^7.3.0" + ember-template-lint@^1.0.0-beta.5: version "1.1.0" resolved "https://registry.yarnpkg.com/ember-template-lint/-/ember-template-lint-1.1.0.tgz#312e101728452bf082f54cbe429ed9b52273ba64" @@ -4000,6 +4033,55 @@ es-to-primitive@^1.2.0: is-date-object "^1.0.1" is-symbol "^1.0.2" +es5-ext@^0.10.35, es5-ext@^0.10.9, es5-ext@~0.10.14: + version "0.10.47" + resolved "https://registry.yarnpkg.com/es5-ext/-/es5-ext-0.10.47.tgz#d24232e1380daad5449a817be19bde9729024a11" + integrity sha512-/1TItLfj+TTfWoeRcDn/0FbGV6SNo4R+On2GGVucPU/j3BWnXE2Co8h8CTo4Tu34gFJtnmwS9xiScKs4EjZhdw== + dependencies: + es6-iterator "~2.0.3" + es6-symbol "~3.1.1" + next-tick "1" + +es6-iterator@~2.0.1, es6-iterator@~2.0.3: + version "2.0.3" + resolved "https://registry.yarnpkg.com/es6-iterator/-/es6-iterator-2.0.3.tgz#a7de889141a05a94b0854403b2d0a0fbfa98f3b7" + integrity sha1-p96IkUGgWpSwhUQDstCg+/qY87c= + dependencies: + d "1" + es5-ext "^0.10.35" + es6-symbol "^3.1.1" + +es6-map@^0.1.5: + version "0.1.5" + resolved "https://registry.yarnpkg.com/es6-map/-/es6-map-0.1.5.tgz#9136e0503dcc06a301690f0bb14ff4e364e949f0" + integrity sha1-kTbgUD3MBqMBaQ8LsU/042TpSfA= + dependencies: + d "1" + es5-ext "~0.10.14" + es6-iterator "~2.0.1" + es6-set "~0.1.5" + es6-symbol "~3.1.1" + event-emitter "~0.3.5" + +es6-set@~0.1.5: + version "0.1.5" + resolved "https://registry.yarnpkg.com/es6-set/-/es6-set-0.1.5.tgz#d2b3ec5d4d800ced818db538d28974db0a73ccb1" + integrity sha1-0rPsXU2ADO2BjbU40ol02wpzzLE= + dependencies: + d "1" + es5-ext "~0.10.14" + es6-iterator "~2.0.1" + es6-symbol "3.1.1" + event-emitter "~0.3.5" + +es6-symbol@3.1.1, es6-symbol@^3.1.1, es6-symbol@~3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/es6-symbol/-/es6-symbol-3.1.1.tgz#bf00ef4fdab6ba1b46ecb7b629b4c7ed5715cc77" + integrity sha1-vwDvT9q2uhtG7Le2KbTH7VcVzHc= + dependencies: + d "1" + es5-ext "~0.10.14" + escape-html@~1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/escape-html/-/escape-html-1.0.3.tgz#0258eae4d3d0c0974de1c169188ef0051d1d1988" @@ -4160,6 +4242,14 @@ etag@~1.8.1: resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" integrity sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc= +event-emitter@~0.3.5: + version "0.3.5" + resolved "https://registry.yarnpkg.com/event-emitter/-/event-emitter-0.3.5.tgz#df8c69eef1647923c7157b9ce83840610b02cc39" + integrity sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk= + dependencies: + d "1" + es5-ext "~0.10.14" + eventemitter3@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-3.1.0.tgz#090b4d6cdbd645ed10bf750d4b5407942d7ba163" @@ -5543,6 +5633,11 @@ is-regex@^1.0.4: dependencies: has "^1.0.1" +is-regexp@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/is-regexp/-/is-regexp-1.0.0.tgz#fd2d883545c46bac5a633e7b9a09e87fa2cb5069" + integrity sha1-/S2INUXEa6xaYz57mgnof6LLUGk= + is-resolvable@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/is-resolvable/-/is-resolvable-1.1.0.tgz#fb18f87ce1feb925169c9a407c19318a3206ed88" @@ -6272,7 +6367,7 @@ lodash.values@~2.3.0: dependencies: lodash.keys "~2.3.0" -lodash@^4.17.10, lodash@^4.17.4, lodash@^4.3.0, lodash@^4.6.1: +lodash@^4.17.10, lodash@^4.17.11, lodash@^4.17.2, lodash@^4.17.4, lodash@^4.3.0, lodash@^4.6.1: version "4.17.11" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d" integrity sha512-cQKh8igo5QUhZ7lg38DYWAxMvjSAKG0A8wGSVimP07SIUEK2UO+arSRKbRZWtelMtN5V0Hkwh5ryOto/SshYIg== @@ -6734,6 +6829,11 @@ neo-async@^2.5.0: resolved "https://registry.yarnpkg.com/neo-async/-/neo-async-2.6.0.tgz#b9d15e4d71c6762908654b5183ed38b753340835" integrity sha512-MFh0d/Wa7vkKO3Y3LlacqAEeHK0mckVqzDieUKTT+KGxi+zIpeVsFxymkIiRpbpDziHc290Xr9A1O4Om7otoRA== +next-tick@1: + version "1.0.0" + resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c" + integrity sha1-yobR/ogoFpsBICCOPchCS524NCw= + nice-try@^1.0.4: version "1.0.5" resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366" @@ -7500,6 +7600,14 @@ querystring@0.2.0: resolved "https://registry.yarnpkg.com/querystring/-/querystring-0.2.0.tgz#b209849203bb25df820da756e747005878521620" integrity sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA= +quibble@^0.5.5: + version "0.5.5" + resolved "https://registry.yarnpkg.com/quibble/-/quibble-0.5.5.tgz#669fb731520a923e0a98f8076b7eb55e409f73f9" + integrity sha512-cIePu3BtGlaTW1bjFgBcLT6QMxD8PtnZDCmPJUzO+RepIz8GuXsmZIEPGFjlPxzG9zfIj4nNLPxBDlUbvr9ESg== + dependencies: + lodash "^4.17.2" + resolve "^1.7.1" + quick-temp@^0.1.2, quick-temp@^0.1.3, quick-temp@^0.1.5, quick-temp@^0.1.8: version "0.1.8" resolved "https://registry.yarnpkg.com/quick-temp/-/quick-temp-0.1.8.tgz#bab02a242ab8fb0dd758a3c9776b32f9a5d94408" @@ -8541,6 +8649,14 @@ string_decoder@~1.1.1: dependencies: safe-buffer "~5.1.0" +stringify-object-es5@^2.5.0: + version "2.5.0" + resolved "https://registry.yarnpkg.com/stringify-object-es5/-/stringify-object-es5-2.5.0.tgz#057c3c9a90a127339bb9d1704a290bb7bd0a1ec5" + integrity sha1-BXw8mpChJzObudFwSikLt70KHsU= + dependencies: + is-plain-obj "^1.0.0" + is-regexp "^1.0.0" + strip-ansi@^3.0.0, strip-ansi@^3.0.1: version "3.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-3.0.1.tgz#6a385fb8853d952d5ff05d0e8aaf94278dc63dcf" @@ -8683,6 +8799,22 @@ terser@^3.7.5, terser@^3.8.1: source-map "~0.6.1" source-map-support "~0.5.6" +testdouble-qunit@^2.1.1: + version "2.1.1" + resolved "https://registry.yarnpkg.com/testdouble-qunit/-/testdouble-qunit-2.1.1.tgz#3f5e8ff7a6bdeb9d75941e88100efeef823089a2" + integrity sha512-1+2oA5oVogN9PwbshcnC4TneJhRgHSRi1pTPaDfDTM+w41yrLJzH95GK249BvELPs3Tmd9JQ6xkr8fs5DKsmow== + +testdouble@^3.0.0, testdouble@^3.5.2: + version "3.9.3" + resolved "https://registry.yarnpkg.com/testdouble/-/testdouble-3.9.3.tgz#d83f86a41f9ad8e23d1e6a2252f5fa79177de826" + integrity sha512-gzTDEd6azy1hUw4OP8TjDThR8OealIuXI8xaxk/JtXSW1zBkxg/BShOHdxWy2N7+kDg3cB8y/LVtugfRPLveqA== + dependencies: + es6-map "^0.1.5" + lodash "^4.17.11" + quibble "^0.5.5" + stringify-object-es5 "^2.5.0" + theredoc "^1.0.0" + testem@^2.9.2: version "2.14.0" resolved "https://registry.yarnpkg.com/testem/-/testem-2.14.0.tgz#418a9a15843f68381659c6a486abb4ea48d06c29" @@ -8727,6 +8859,11 @@ text-table@~0.2.0: resolved "https://registry.yarnpkg.com/textextensions/-/textextensions-2.4.0.tgz#6a143a985464384cc2cff11aea448cd5b018e72b" integrity sha512-qftQXnX1DzpSV8EddtHIT0eDDEiBF8ywhFYR2lI9xrGtxqKN+CvLXhACeCIGbCpQfxxERbrkZEFb8cZcDKbVZA== +theredoc@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/theredoc/-/theredoc-1.0.0.tgz#bcace376af6feb1873efbdd0f91ed026570ff062" + integrity sha512-KU3SA3TjRRM932jpNfD3u4Ec3bSvedyo5ITPI7zgWYnKep7BwQQaxlhI9qbO+lKJoRnoAbEVfMcAHRuKVYikDA== + through2@^2.0.0: version "2.0.5" resolved "https://registry.yarnpkg.com/through2/-/through2-2.0.5.tgz#01c1e39eb31d07cb7d03a96a70823260b23132cd"