How can I unit test a method that results in an action

In my Ember app, I have a simple form component with the following behavior (snippet):

... let searchText = this.get('searchText') && this.get('searchText').trim().toLowerCase(); this.sendAction('searchTextChanged', searchText); ... 

How can I unit test use this function in qunit test? I can’t understand how to listen to the result of an action. I use the test functionality that comes with ember-cli - qunit, helpers, etc.

+1
source share
1 answer

Ember has a guide on how to do this here . I will put the code here if the manual changes.

Given this component:

 App.MyFooComponent = Ember.Component.extend({ layout:Ember.Handlebars.compile("<button {{action 'doSomething'}}></button>"), actions: { doSomething: function() { this.sendAction('internalAction'); } } }); 

You have verified the action as follows:

 moduleForComponent('my-foo', 'MyFooComponent'); test('trigger external action when button is clicked', function() { // tell our test to expect 1 assertion expect(1); // component instance var component = this.subject(); // component dom instance var $component = this.append(); var targetObject = { externalAction: function() { // we have the assertion here which will be // called when the action is triggered ok(true, 'external Action was called!'); } }; // setup a fake external action to be called when // button is clicked component.set('internalAction', 'externalAction'); // set the targetObject to our dummy object (this // is where sendAction will send its action to) component.set('targetObject', targetObject); // click the button click('button'); }); 
+3
source

Source: https://habr.com/ru/post/1238565/


All Articles