File tree 2 files changed +93
-1
lines changed
tests/integration/helpers
2 files changed +93
-1
lines changed Original file line number Diff line number Diff line change 1
1
import { helper } from '@ember/component/helper' ;
2
+ import { assert } from '@ember/debug' ;
2
3
import { taskHelperClosure } from 'ember-concurrency/-private/helpers' ;
3
4
5
+ function maybeReportError ( onError ) {
6
+ return function ( e ) {
7
+ if ( typeof onError === 'function' ) {
8
+ onError ( e ) ;
9
+ } else if ( onError === null ) {
10
+ // Do nothing
11
+ } else {
12
+ assert (
13
+ `The onError argument passed to the \`perform\` helper should be a function or null; you passed ${ onError } ` ,
14
+ false
15
+ ) ;
16
+ }
17
+ } ;
18
+ }
19
+
4
20
export function performHelper ( args , hash ) {
5
- return taskHelperClosure ( 'perform' , 'perform' , args , hash ) ;
21
+ let perform = taskHelperClosure ( 'perform' , 'perform' , args , hash ) ;
22
+
23
+ if ( hash && typeof hash . onError !== 'undefined' ) {
24
+ return function ( ...innerArgs ) {
25
+ try {
26
+ let taskInstance = perform ( ...innerArgs ) ;
27
+ return taskInstance . catch ( maybeReportError ( hash . onError ) ) ;
28
+ // eslint-disable-next-line no-empty
29
+ } catch {
30
+ maybeReportError ( hash . onError ) ;
31
+ }
32
+ } ;
33
+ } else {
34
+ return perform ;
35
+ }
6
36
}
7
37
8
38
export default helper ( performHelper ) ;
Original file line number Diff line number Diff line change @@ -35,4 +35,66 @@ module('Integration | helpers | perform', function (hooks) {
35
35
36
36
assert . ok ( true ) ;
37
37
} ) ;
38
+
39
+ test ( 'can pass onError=null to have it swallow errors thrown from task' , async function ( assert ) {
40
+ assert . expect ( 1 ) ;
41
+
42
+ this . owner . register (
43
+ 'component:test-swallow-error' ,
44
+ Component . extend ( {
45
+ errorGeneratingTask : task ( function * ( ) {
46
+ throw new Error ( 'You should not see me!' ) ;
47
+ } ) ,
48
+ } )
49
+ ) ;
50
+
51
+ this . owner . register (
52
+ 'template:components/test-swallow-error' ,
53
+ hbs `
54
+ <button {{on 'click' (perform this.errorGeneratingTask onError=null)}}>
55
+ I create an error!
56
+ </button>
57
+ `
58
+ ) ;
59
+
60
+ await render ( hbs `<TestSwallowError />` ) ;
61
+
62
+ await click ( 'button' ) ;
63
+
64
+ assert . ok ( true ) ;
65
+ } ) ;
66
+
67
+ test ( 'can pass onError=someFn to have it call someFn(e)' , async function ( assert ) {
68
+ assert . expect ( 2 ) ;
69
+
70
+ let error = null ;
71
+
72
+ this . owner . register (
73
+ 'component:test-swallow-error' ,
74
+ Component . extend ( {
75
+ errorGeneratingTask : task ( function * ( ) {
76
+ throw new Error ( 'You should not see me!' ) ;
77
+ } ) ,
78
+ errorReport ( e ) {
79
+ error = e ;
80
+ } ,
81
+ } )
82
+ ) ;
83
+
84
+ this . owner . register (
85
+ 'template:components/test-swallow-error' ,
86
+ hbs `
87
+ <button {{on 'click' (perform this.errorGeneratingTask onError=this.errorReport)}}>
88
+ I create an error!
89
+ </button>
90
+ `
91
+ ) ;
92
+
93
+ await render ( hbs `<TestSwallowError />` ) ;
94
+
95
+ await click ( 'button' ) ;
96
+
97
+ assert . ok ( true ) ;
98
+ assert . ok ( error instanceof Error ) ;
99
+ } ) ;
38
100
} ) ;
You can’t perform that action at this time.
0 commit comments