Skip to content

Commit 9b0207d

Browse files
Subtletreeknownasilya
authored andcommitted
feat: Allow custom version comparison function (#57)
* Allow custom version comparison function * Document updateNeeded in readme
1 parent 2a0b172 commit 9b0207d

3 files changed

Lines changed: 59 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ A convention-based version update notifier. Use it to notify users already on th
3434
* `showReload` - _true_ shows a reload button the user can click to refresh. _false_ hides the button. **default: true**
3535
* `reloadButtonText` - Sets the text for the default reload button. **default: "Reload"**
3636
* `onNewVersion(newVersion, oldVersion)` - a closure action that is called whenever a new version is detected. You can use this to track the version status elsewhere in your app (outside the component).
37+
* `updateNeeded(oldVersion, newVersion)` - a function that is called to check if an update message should be shown. For example, a function could be passed that only shows a message on major version changes. **default: Always show message on any version change**
3738

3839
```handlebars
3940
{{new-version-notifier updateInterval=<value> versionFileName="<value>" updateMessage="<value>" showReload=true}}

addon/components/new-version-notifier/component.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default Component.extend({
7575
const currentVersion = this.get('version');
7676
const newVersion = res && res.trim();
7777

78-
if (currentVersion && newVersion !== currentVersion) {
78+
if (this.updateNeeded(currentVersion, newVersion)) {
7979
const message = this.get('updateMessage')
8080
.replace('{{oldVersion}}', currentVersion)
8181
.replace('{{newVersion}}', newVersion);
@@ -104,6 +104,9 @@ export default Component.extend({
104104
}
105105
}),
106106

107+
updateNeeded(currentVersion, newVersion) {
108+
return currentVersion && newVersion !== currentVersion;
109+
},
107110

108111
actions: {
109112
reload() {

tests/integration/components/new-version-notifier-test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,58 @@ module('Integration | Component | new version notifier', function(hooks) {
150150
await waitUntil(() => called, { timeout: 150 });
151151
assert.ok(onErrorCalled, 'onError was called');
152152
});
153+
154+
test('it accepts a custom updateNeeded function', async function(assert) {
155+
assert.expect(5);
156+
let done = assert.async(2);
157+
158+
let callCount = 0;
159+
160+
this.server.get('/VERSION.txt', function(){
161+
++callCount
162+
switch (callCount) {
163+
case 0:
164+
return 'v1.0.0';
165+
case 1:
166+
return 'v1.0.1';
167+
case 2:
168+
return 'v1.0.2';
169+
case 3:
170+
return 'v1.1.0';
171+
default:
172+
return null;
173+
}
174+
});
175+
176+
set(this, "onNewVersion", (newVersion, oldVersion) => {
177+
assert.equal(newVersion, "v1.1.0", "newVersion v1.1.0 is sent to onNewVersion");
178+
assert.equal(oldVersion, "v1.0.2", "oldVersion v1.0.2 is sent to onNewVersion");
179+
done();
180+
});
181+
set(this, "enableInTests", true);
182+
183+
set(this, "updateNeeded", function(currentVersion, newVersion) {
184+
if (!currentVersion) { return false }
185+
186+
// Only compare major and minor version number
187+
let currentV = currentVersion.substr(0, currentVersion.lastIndexOf("."));
188+
let newV = newVersion.substr(0, newVersion.lastIndexOf("."));
189+
190+
return newV !== currentV;
191+
});
192+
193+
render(hbs`{{new-version-notifier updateNeeded=updateNeeded updateInterval=100 enableInTests=enableInTests onNewVersion=onNewVersion}}`);
194+
195+
await waitUntil(() => callCount === 1, { timeout: 95 });
196+
assert.equal(callCount, 1, "1 call was made");
197+
198+
await waitUntil(() => callCount === 2, { timeout: 190 });
199+
assert.equal(callCount, 2);
200+
201+
await waitUntil(() => callCount === 3, { timeout: 190 });
202+
assert.equal(callCount, 3);
203+
204+
set(this, "enableInTests", false); // stop the loop from continuing
205+
done();
206+
});
153207
});

0 commit comments

Comments
 (0)