Preact: child component with state as prop does not rerender on state change if in array of components - state

If currentQuestion in the state of the parent Component gets updated it rerenders the parent component perfectly fine with the new state.
The problem is, that the child component rendered from the view array seems to not get the updated state which is passed as a prop and so it does not rerender.
If i move render the Child component directly in the render function of the parent (not from an array) everything works as expected.
I dont find the problem here, would be awesome if someone could help.
Parent
class App extends Component {
constructor(props) {
super(props);
this.state = {
currentQuestion: 0
};
// Child components should be stored in an array
this.views = [
<Questions
currentQuestion={this.state.currentQuestion}
/>
];
}
render() {
return <div className={style.quiz}>
// Clicking this button, the parent gets rerendered properly with new state
<button onClick={() => this.setState({currentQuestion: 3})}>Test</button>
<div className={style.views}>
{this.views.map(view => view)}
</div>
</div>;
}
}
Child
class Questions extends Component {
constructor(props) {
super(props);
}
render() {
// If the state of the parent component gets updated (button click), the props in here should update and therefore this should be called, which is not the case
return <div>
{this.props.currentQuestion}
</div>
}
}

The child components are not being updated when the parent component is updated because the child components are instantiated only when the parent's constructor is run.
A way to solve this is to make sure the child components are also updated every time the parent updates (when this.setState({ currentQuestion: }) is called).
render() {
return <div className={style.quiz}>
...
<Questions currentQuestion={this.state.currentQuestion} />
...
</div>;
}

Related

Vue.js test:unit with test-utils and Jest : Nested component -Is it possible to mock the $emit(function) from child component?

Given nested components
the Heading.vue component
{{ $t("lang.views.home.heading.btn__listen") }}
play_arrow
The nested child component AuioPlayer.vue
<template>
<div style="display: inline-block;">
<v-btn id="playPauseBtn">
...
</v-btn>
<v-btn id="stopBtn" outline icon class="inline teal--text" #click.native="stop()">
<v-icon>stop</v-icon>
</v-btn>
<v-btn id="muteBtn">
...
</v-btn>>
</div>
</template>
<script>
...
methods: {
stop() {
this.$data._howl.stop();
this.$emit("playerStop");
},
...
</script>
Is it possible to test the parent Heading.vue , using shallowMount() mocking the $emit("playerStop") event ... ?
it("should display LISTEN button on child component audioplayer event stop()", () => {
// given
wrapper = shallowMount(Heading, { router, i18n });
wrapper.vm.listening = true;
// when
// audioplayer child component should be stubbed
const audioplayer = wrapper.find('#audioplayer');
console.log(audioplayer.html());
// mock the $emit(playerStop) from the child audioplayer stub
expect(wrapper.vm.listening).toBe(false);
});
UPDATE
I trued 2 solutions without any success
1 / using a spy function
it("should display LISTEN button on child component audioplayer event stop()", () => {
// given
wrapper = shallowMount(Heading, { router, i18n });
const spy = jest.fn();
// wrapper.vm.$on('stopPlayer', spy); // focus on the call of listener
wrapper.setData({ listening: true });
const audioplayer = wrapper.find('audioplayer-stub');
// when
audioplayer.trigger('stopPlayer');
// then
expect(spy).toHaveBeenCalledTimes(1);
expect(wrapper.vm.listening).toBe(false);
});
2 / Using an async $emit()
it("should display LISTEN button on child component audioplayer event stop()", async () => {
// given
wrapper = shallowMount(Heading, { router, i18n });
wrapper.setData({ listening: true });
const audioplayer = wrapper.find('audioplayer-stub');
// when
audioplayer.vm.$emit('stopPlayer');
await wrapper.vm.$nextTick();
// then
expect(wrapper.vm.listening).toBe(false);
});
In both cases it seems that if I trigger or emit from the sub-component nothing happen...
As a matter of fact, the emit() should be done from a stop button in the sub-component which is not stubbed at this level ..
Is there anyway to stub it ?
I want to avoid a mount ... using shallowMount should be sufficient at this level of tets ...
thanks for feedback
SOLVED ... this is one of the traps to avoid while unit testing vue.js : What should I test ?, not testing the wrong thing....
using test-utils w shallowMount, I should not test for the emi() event from a stubbed component ( this should be tested later within this component) I should only test the method which will be called ...
In tis case
methods: {
playerStop() {
this.listening = false;
}
}
tested simply with
it("method playerStop should tpggle listening to false", async () => {
// given
wrapper = shallowMount(Heading, { router, i18n });
wrapper.setData({ listening: true });
// when
wrapper.vm.playerStop();
const playBtn = wrapper.find('#playBtn')
// then
expect(wrapper.vm.listening).toBe(false);
});

How to unit test a Redux action in a component inside a connected Redux component with Jest

I'm using jest and enzyme to unit test my React application and I'm struggling with testing connected components.
I do have a simple component which the following logic:
class LoginPage extends React.Component {
componentDidMount() {
if (!this.props.reduxReducer.appBootstrapped) {
this.props.dispatch(ReduxActions.fadeOutAndRemoveSplashScreen(500));
}
}
render() {
return (
<div data-page="login-page" >
<div>This is the login page.</div>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
reduxReducer: state.reduxReducer
}
};
export default connect(mapStateToProps, null)(LoginPage);
So, this is a component which displays a <div /> element containing some text, but the important part that I want to test is that when the component is mounted, an action is dispatched to hide the splash screen.
I want this only to happen when the application is not bootstrapped.
I do have a simple unit test to test that the component is rendered:
describe("[LoginPage Component]", () => {
it("Renders without a problem.", () => {
// Act.
const wrapper = mount(
<LoginPage store={ reduxStore } />
);
// Assert.
expect(wrapper.find("div[data-page=\"login-page\"]").length).toBe(1);
});
});
The reduxStore property is my actual redux store, created with the following code:
const reduxStore = createStore(
combineReducers(
{
reduxReducer
}
)
);
Now, how can I test the componentDidMount() method, and more in special, test that the redux action fadeOutAndRemoveSplashScreen() is only called when the application is not bootstrapped yet.
I do think that I need to mock my redux store, however, I'm a newbie on this and don't now how to get started, so an example will be highly appreciated.
If any other thoughts on my implementation, feel free to provide some advice.
Kind regards
I wouldn't use the raw dispatch method to send off an action. I would use mapDispatchToProps. This makes your action directly available in your component props - here we use ES6 destructing as a short hand in the connect method.
Then instead of mocking the redux store I would just test your component without it. Try adding an export to your class (first line). For example:
export class LoginPage extends React.Component {
componentDidMount() {
if (!this.props.reduxReducer.appBootstrapped) {
// make sure that you are using this.props.action() not
// just the action(), which is not connected to redux
this.props.fadeOutAndRemoveSplashScreen(500);
}
}
render() {
return (
<div data-page="login-page" >
<div>This is the login page.</div>
</div>
);
}
}
const mapStateToProps = (state) => {
return {
reduxReducer: state.reduxReducer
}
};
export default connect(mapStateToProps, {
fadeOutAndRemoveSplashScreen: ReduxActions.fadeOutAndRemoveSplashScreen
})(LoginPage);
Then in your test instead of importing the connected component, import the class:
import ConnectedLoginPage, { LoginPage } from '/path/to/component';
Then simply pass the LoginPage whatever props you want to test with. So we will set your appBooststrapped to false, and then pass the action as a sinon spy:
const spy = sinon.spy();
const reduxReducer = {
appBootstrapped: false, // or true
}
const wrapper = mount(
<LoginPage reduxReducer={reduxReducer} fadeOutAndRemoveSplashScreen={spy} />
);
// test that the spy was called
expect(spy.callCount).to.equal(1);
This makes the test much simpler, and more importantly you are testing the component behavior - not Redux.

How to add a class to the selected component?

I have a list of components (all the same)
<ul class="search-results">
{{#each res as |item|}}
{{search-result item=item}}
{{/each}}
</ul>
I'd like to add a class on the clicked element, and, when a new element is clicked, the old element should become "unclicked" (aka, remove the class).
Which is the best way to obtain this result?
Add activeItem to controller (wrapper component) and send it down to all search-result components. Activate item through sending action activate up.
// template
<ul class="search-results">
{{#each res as |item|}}
{{search-result item=item activeItem=activeItem activate="activate"}}
{{/each}}
</ul>
// controller (wrapper component)
activeItem: null,
actions: {
activate(item) {
this.set('activeItem', item);
}
}
// search-result component
activeItem: null,
isActive: Ember.computed('item', 'activeItem', function() {
return (this.get('item') === this.get('activeItem'));
}),
click() {
this.sendAction('activate', this.get('item'));
}

Mocking $modal in AngularJS unit tests

I'm writing a unit test for a controller that fires up a $modal and uses the promise returned to execute some logic. I can test the parent controller that fires the $modal, but I can't for the life of me figure out how to mock a successful promise.
I've tried a number of ways, including using $q and $scope.$apply() to force the resolution of the promise. However, the closest I've gotten is putting together something similar to the last answer in this SO post;
I've seen this asked a few times with the "old" $dialog modal.
I can't find much on how to do it with the "new" $dialog modal.
Some pointers would be tres appreciated.
To illustrate the problem I'm using the example provided in the UI Bootstrap docs, with some minor edits.
Controllers (Main and Modal)
'use strict';
angular.module('angularUiModalApp')
.controller('MainCtrl', function($scope, $modal, $log) {
$scope.items = ['item1', 'item2', 'item3'];
$scope.open = function() {
$scope.modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
resolve: {
items: function() {
return $scope.items;
}
}
});
$scope.modalInstance.result.then(function(selectedItem) {
$scope.selected = selectedItem;
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});
};
})
.controller('ModalInstanceCtrl', function($scope, $modalInstance, items) {
$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};
$scope.ok = function() {
$modalInstance.close($scope.selected.item);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
});
The view (main.html)
<div ng-controller="MainCtrl">
<script type="text/ng-template" id="myModalContent.html">
<div class="modal-header">
<h3>I is a modal!</h3>
</div>
<div class="modal-body">
<ul>
<li ng-repeat="item in items">
<a ng-click="selected.item = item">{{ item }}</a>
</li>
</ul>
Selected: <b>{{ selected.item }}</b>
</div>
<div class="modal-footer">
<button class="btn btn-primary" ng-click="ok()">OK</button>
<button class="btn btn-warning" ng-click="cancel()">Cancel</button>
</div>
</script>
<button class="btn btn-default" ng-click="open()">Open me!</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
The test
'use strict';
describe('Controller: MainCtrl', function() {
// load the controller's module
beforeEach(module('angularUiModalApp'));
var MainCtrl,
scope;
var fakeModal = {
open: function() {
return {
result: {
then: function(callback) {
callback("item1");
}
}
};
}
};
beforeEach(inject(function($modal) {
spyOn($modal, 'open').andReturn(fakeModal);
}));
// Initialize the controller and a mock scope
beforeEach(inject(function($controller, $rootScope, _$modal_) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope,
$modal: _$modal_
});
}));
it('should show success when modal login returns success response', function() {
expect(scope.items).toEqual(['item1', 'item2', 'item3']);
// Mock out the modal closing, resolving with a selected item, say 1
scope.open(); // Open the modal
scope.modalInstance.close('item1');
expect(scope.selected).toEqual('item1');
// No dice (scope.selected) is not defined according to Jasmine.
});
});
When you spy on the $modal.open function in the beforeEach,
spyOn($modal, 'open').andReturn(fakeModal);
or
spyOn($modal, 'open').and.returnValue(fakeModal); //For Jasmine 2.0+
you need to return a mock of what $modal.open normally returns, not a mock of $modal, which doesn’t include an open function as you laid out in your fakeModal mock. The fake modal must have a result object that contains a then function to store the callbacks (to be called when the OK or Cancel buttons are clicked on). It also needs a close function (simulating an OK button click on the modal) and a dismiss function (simulating a Cancel button click on the modal). The close and dismiss functions call the necessary call back functions when called.
Change the fakeModal to the following and the unit test will pass:
var fakeModal = {
result: {
then: function(confirmCallback, cancelCallback) {
//Store the callbacks for later when the user clicks on the OK or Cancel button of the dialog
this.confirmCallBack = confirmCallback;
this.cancelCallback = cancelCallback;
}
},
close: function( item ) {
//The user clicked OK on the modal dialog, call the stored confirm callback with the selected item
this.result.confirmCallBack( item );
},
dismiss: function( type ) {
//The user clicked cancel on the modal dialog, call the stored cancel callback
this.result.cancelCallback( type );
}
};
Additionally, you can test the cancel dialog case by adding a property to test in the cancel handler, in this case $scope.canceled:
$scope.modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$scope.canceled = true; //Mark the modal as canceled
$log.info('Modal dismissed at: ' + new Date());
});
Once the cancel flag is set, the unit test will look something like this:
it("should cancel the dialog when dismiss is called, and $scope.canceled should be true", function () {
expect( scope.canceled ).toBeUndefined();
scope.open(); // Open the modal
scope.modalInstance.dismiss( "cancel" ); //Call dismiss (simulating clicking the cancel button on the modal)
expect( scope.canceled ).toBe( true );
});
To add to Brant's answer, here is a slightly improved mock that will let you handle some other scenarios.
var fakeModal = {
result: {
then: function (confirmCallback, cancelCallback) {
this.confirmCallBack = confirmCallback;
this.cancelCallback = cancelCallback;
return this;
},
catch: function (cancelCallback) {
this.cancelCallback = cancelCallback;
return this;
},
finally: function (finallyCallback) {
this.finallyCallback = finallyCallback;
return this;
}
},
close: function (item) {
this.result.confirmCallBack(item);
},
dismiss: function (item) {
this.result.cancelCallback(item);
},
finally: function () {
this.result.finallyCallback();
}
};
This will allow the mock to handle situations where...
You use the modal with the .then(), .catch() and .finally() handler style instead passing 2 functions (successCallback, errorCallback) to a .then(), for example:
modalInstance
.result
.then(function () {
// close hander
})
.catch(function () {
// dismiss handler
})
.finally(function () {
// finally handler
});
Since modals use promises you should definitely use $q for such things.
Code becomes:
function FakeModal(){
this.resultDeferred = $q.defer();
this.result = this.resultDeferred.promise;
}
FakeModal.prototype.open = function(options){ return this; };
FakeModal.prototype.close = function (item) {
this.resultDeferred.resolve(item);
$rootScope.$apply(); // Propagate promise resolution to 'then' functions using $apply().
};
FakeModal.prototype.dismiss = function (item) {
this.resultDeferred.reject(item);
$rootScope.$apply(); // Propagate promise resolution to 'then' functions using $apply().
};
// ....
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
fakeModal = new FakeModal();
MainCtrl = $controller('MainCtrl', {
$scope: scope,
$modal: fakeModal
});
}));
// ....
it("should cancel the dialog when dismiss is called, and $scope.canceled should be true", function () {
expect( scope.canceled ).toBeUndefined();
fakeModal.dismiss( "cancel" ); //Call dismiss (simulating clicking the cancel button on the modal)
expect( scope.canceled ).toBe( true );
});
Brant's answer was clearly awesome, but this change made it even better for me:
fakeModal =
opened:
then: (openedCallback) ->
openedCallback()
result:
finally: (callback) ->
finallyCallback = callback
then in the test area:
finallyCallback()
expect (thing finally callback does)
.toEqual (what you would expect)

Ember.js v1.2.0 - Ember.Component not firing 'click' handler, returning null for this.$()

I'm using Ember v1.2.0 along with Handlebars v1.0.0 and jQuery v2.0.2 and I started to use Ember.Component and replace some views I created through components (for example a custom dropdown element) and it felt like the right thing to do, but unfortunately it does not work as I expected.
this is my Handlebars file, placed under `templates/components/my-dropdown:
<div class="dropdown__header" {{action 'toggle'}}>
<i {{bind-attr class=view.iconClass}}></i>{{view.displayText}}
</div>
<div class="dropdown__caret"></div>
<ul class="dropdown__body">
{{yield}}
</ul>
this is the corresponding Ember.Component class:
App.MyDropdownComponent = Ember.Component.extend({
classNames: 'dropdown'.w(),
toggleList: function () {
//var $this = this.$(); // returns null (!!!)
var $this = this.$('.' + this.get('classNames').join(' .')); // returns the expected object
if($this.hasClass('open')) {
$this.removeClass('open');
} else {
$this.addClass('open');
}
// Note: I can't work with classNameBindings and toggleProperty() because the classes
// could also be accessed through other code...
},
click: function (event) {
alert('heureka!'); // never fired!
},
actions: {
toggle: function () {
this.toggleList(); // fired as expected
}
}
});
is this expected behaviour of an Ember.Component?