-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit test component examples? #183
Comments
Let us know how that works out! If you're testing redux, I'd make sure to do unit tests on the reducers themselves. It's easier but it's also way more robust. |
Yea i got this working. Unfortunately, there's no code to share because it's closed source :( but here's an overview of what needs to happen:
Here's a minimal test example that uses karma and mocha: import 'angularMocks';
import angular from 'angular';
import 'ng-redux';
import appReducers from '../../reducers';
import './user-completion.component';
import '../../app';
// Setup angular. Note that we need a working ngRedux for the template tests
angular.module('testConnect', ['ngRedux']).config([
'$ngReduxProvider',
function($ngReduxProvider) {
$ngReduxProvider.createStoreWith(appReducers, []);
},
]);
describe.only('component', function() {
let ngRedux;
beforeEach(angular.mock.module('testConnect'));
beforeEach(angular.mock.module('componentUnderTest'));
beforeEach(angular.mock.inject(function($ngRedux) {
ngRedux = $ngRedux;
}));
describe('with $compile', function() {
let element;
let scope;
beforeEach(
angular.mock.inject(function($rootScope, $compile) {
scope = $rootScope.$new();
element = angular.element('<component-under-test></component-under-test>');
element = $compile(element)(scope);
scope.$apply();
})
);
it('should show missing username', function() {
ngRedux.dispatch(updateState({
missing: {
username: true,
email: false,
},
}));
scope.$apply();
const result = element.html().toString();
expect(result).to.match(/Username/);
});
});
}); |
One of the things that I noticed, however, is that it's not obvious how to do fuller integration tests i.e., a test where everything is real except for AJAX requests. In our code base we are already doing this when testing sagas. Unfortunately, for this to work we use So... to make these types of integration tests work with templates, we need the ngRedux magic of reduxifying angular components and the Update: I suppose there's this: https://stackoverflow.com/questions/35622588/how-to-reset-the-state-of-a-redux-store/35641992#35641992 The downside is that tests start to get a lot of boilerplate |
Have you guys had any luck unit testing components? (See for example, https://puigcerber.com/2016/02/07/how-to-test-angular-1-5-components/)
I've currently configured angular 1.5, ngtemplate-loader, and webpack to load the HTML partials correctly in karma but tests are erring out with the following ngRedux error:
Update: oh wait, I probably have to configure ngRedux in tests as well (doh)
The text was updated successfully, but these errors were encountered: