|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */
|
2 | 2 | import { tracked } from '@glimmer/tracking';
|
3 | 3 | import { settled } from '@ember/test-helpers';
|
| 4 | +import { waitFor } from '@ember/test-waiters'; |
4 | 5 | import { module, test } from 'qunit';
|
5 | 6 | import { setupTest } from 'ember-qunit';
|
6 | 7 |
|
7 |
| -import { restartableTask, timeout } from 'ember-concurrency'; |
| 8 | +import { dropTask, restartableTask, timeout } from 'ember-concurrency'; |
8 | 9 | import { taskFor } from 'ember-concurrency-ts';
|
9 | 10 | import { trackedTask } from 'ember-resources/util/ember-concurrency';
|
10 | 11 |
|
@@ -168,6 +169,37 @@ module('useTask', function () {
|
168 | 169 | assert.false(foo.search.isRunning);
|
169 | 170 | assert.strictEqual(foo.search.value, null);
|
170 | 171 | });
|
| 172 | + |
| 173 | + test('it returns correct task value when tasks have been dropped', async function (assert) { |
| 174 | + class Test { |
| 175 | + @tracked input = 'initial value'; |
| 176 | + |
| 177 | + search = trackedTask(this, taskFor(this._search), () => [this.input]); |
| 178 | + |
| 179 | + @dropTask |
| 180 | + @waitFor |
| 181 | + *_search(input: string) { |
| 182 | + yield new Promise((resolve) => setTimeout(() => resolve(''))); |
| 183 | + |
| 184 | + return input; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + let foo = new Test(); |
| 189 | + |
| 190 | + // task is initiated upon first access |
| 191 | + foo.search.value; |
| 192 | + |
| 193 | + // immediately start another task (this will be dropped/cancelled) |
| 194 | + foo.input = 'updated value'; |
| 195 | + foo.search.value; |
| 196 | + |
| 197 | + await settled(); |
| 198 | + |
| 199 | + assert.strictEqual(foo.search.value, 'initial value', 'returns value from first task'); |
| 200 | + assert.true(foo.search.isFinished); |
| 201 | + assert.false(foo.search.isRunning); |
| 202 | + }); |
171 | 203 | });
|
172 | 204 | });
|
173 | 205 | });
|
0 commit comments