import { module , test } from 'qunit' ; import EmberObject from '@ember/object' ; import { run } from '@ember/runloop' ; import { task , restartableTask , dropTask , keepLatestTask , enqueueTask } from 'ember-concurrency-decorators' ; module ( 'Unit | decorators' , function ( ) { test ( 'Basic decorators functionality' , function ( assert ) { assert . expect ( 5 ) ; class TestSubject extends EmberObject { @ task doStuff = function * ( ) { yield ; return 123 ; } ; @ restartableTask a = function * ( ) { yield ; return 456 ; } ; @ keepLatestTask b = function * ( ) { yield ; return 789 ; } ; @ dropTask c = function * ( ) { yield ; return 12 ; } ; @ enqueueTask d = function * ( ) { yield ; return 34 ; } ; @ task ( { restartable : true } ) restartable = function * ( ) { } ; @ task ( { enqueue : true } ) enqueue = function * ( ) { } ; @ task ( { drop : true } ) drop = function * ( ) { } ; @ task ( { keepLatest : true } ) keepLatest = function * ( ) { } ; @ task ( { evented : true } ) evented = function * ( ) { } ; @ task ( { debug : true } ) debug = function * ( ) { } ; @ task ( { on : 'hi' } ) on = function * ( ) { } ; @ task ( { cancelOn : 'bye' } ) cancelOn = function * ( ) { } ; @ task ( { maxConcurrency : 1 } ) maxConcurrency = function * ( ) { } ; @ task ( { group : 'foo' } ) group = function * ( ) { } ; } let subject ! ; run ( ( ) => { subject = TestSubject . create ( ) ; subject . get ( 'doStuff' ) . perform ( ) ; subject . get ( 'a' ) . perform ( ) ; subject . get ( 'b' ) . perform ( ) ; subject . get ( 'c' ) . perform ( ) ; subject . get ( 'd' ) . perform ( ) ; } ) ; assert . equal ( subject . get ( 'doStuff.last.value' ) , 123 ) ; assert . equal ( subject . get ( 'a.last.value' ) , 456 ) ; assert . equal ( subject . get ( 'b.last.value' ) , 789 ) ; assert . equal ( subject . get ( 'c.last.value' ) , 12 ) ; assert . equal ( subject . get ( 'd.last.value' ) , 34 ) ; } ) ; test ( 'Encapsulated tasks' , function ( assert ) { assert . expect ( 1 ) ; class TestSubject extends EmberObject { @ task encapsulated = { privateState : 56 , * perform ( ) { yield ; return this . privateState ; } } ; } let subject ! ; run ( ( ) => { subject = TestSubject . create ( ) ; subject . get ( 'encapsulated' ) . perform ( ) ; } ) ; assert . equal ( subject . get ( 'encapsulated.last.value' ) , 56 ) ; } ) ; } ) ;