jasmine

How to test a prop update on React component

拜拜、爱过 提交于 2019-12-17 23:23:58
问题 What is the correct way of unit testing a React component prop update. Here is my test fixture; describe('updating the value', function(){ var component; beforeEach(function(){ component = TestUtils.renderIntoDocument(<MyComponent value={true} />); }); it('should update the state of the component when the value prop is changed', function(){ // Act component.props.value = false; component.forceUpdate(); // Assert expect(component.state.value).toBe(false); }); }); This works fine and the test

How can I spy on a getter property using jasmine?

穿精又带淫゛_ 提交于 2019-12-17 23:22:43
问题 How can I spy on a getter property using jasmine? var o = { get foo() {}, }; spyOn(o, 'foo').and.returnValue('bar'); // Doesn't work. This also does not work AFAICT: spyOn(Object.getOwnPropertyDescriptor(o, 'foo'), 'get').and.returnValue('bar'); 回答1: Since Jasmine 2.6, this has been possible with spyOnProperty. To spy on the accessors for the foo property, do: spyOnProperty(o, 'foo') This allows you to replace the set and/or get accessor functions for an accessor property with a spy function.

Run protractor tests with different window sizes?

萝らか妹 提交于 2019-12-17 23:18:31
问题 I want to start 4 different chrome windows to run the same tests on 4 resolutions. – I know protractor has a feature called multiCapabilities, and I know you can set the window size like this: browser.manage().window().setSize(320, 480); But I don't really find a way to combine these 2. Or is there an easier way to create this behaviour 回答1: A very simple solution that comes in my mind would be to create a for loop in your test file with a switch to make your tests running 4 times with a

Global variables in Karma test runner

大兔子大兔子 提交于 2019-12-17 22:43:58
问题 I have a global variable defined in my main template, which I use to store information bits from the back end, such as the environment context path. I can't move that variable inside a service. How can I expose that variable to Karma when I run the unit tests? 回答1: You either declare that global variable within your test file: var global = "something"; describe('Your test suit', function() { ... }); or add a Javascript file where it's defined to your karma.conf.js file: // list of files /

How can we test non-scope angular controller methods?

若如初见. 提交于 2019-12-17 22:40:13
问题 We have few methods in Angular Controller, which are not on the scope variable. Does anyone know, how we can execute or call those methods inside Jasmine tests? Here is the main code. var testController = TestModule.controller('testController', function($scope, testService) { function handleSuccessOfAPI(data) { if (angular.isObject(data)) { $scope.testData = data; } } function handleFailureOfAPI(status) { console.log("handleFailureOfAPIexecuted :: status :: "+status); } // this is controller

ReferenceError: Can't find variable: require at

时间秒杀一切 提交于 2019-12-17 21:59:32
问题 I have a question about using jasmine with Grunt. I keep getting an error, ReferenceError: Can't find variable: require at whenever I run my jasmine tests. Here is my jasmine entry for my Gruntfile.js : jasmine: { js: { src: jsFiles, options: { specs: 'tests/*_spec.js', helpers: 'tests/helpers/*', vendor: 'vendor/*' } } }, I can run a dummy test without a require just fine, but when I include a require in a test, like so, I get the require error. var testD = require('../src/events_to_actions'

Jasmine.js comparing arrays

自闭症网瘾萝莉.ら 提交于 2019-12-17 21:57:09
问题 Is there a way in jasmine.js to check if two arrays are equal, for example: arr = [1, 2, 3] expect(arr).toBe([1, 2, 3]) expect(arr).toEqual([1, 2, 3]) Neither seems to work. 回答1: Just did the test and it works with toEqual please find my test: http://jsfiddle.net/7q9N7/3/ describe('toEqual', function() { it('passes if arrays are equal', function() { var arr = [1, 2, 3]; expect(arr).toEqual([1, 2, 3]); }); }); Just for information: toBe() versus toEqual(): toEqual() checks equivalence. toBe(),

Angular 2 RC5 Testing Promises in ngOnInit Not Working

只愿长相守 提交于 2019-12-17 20:29:32
问题 I am trying to test a structural directive named MyDirective with Jasmine. The Angular version used is RC5. // Part of the MyDirective class @Directive({selector: '[myDirective]'}) export class MyDirective { constructor(protected templateRef: TemplateRef<any>, protected viewContainer: ViewContainerRef, protected myService: MyService) { } ngOnInit() { this.myService.getData() .then((data) => { if (!MyService.isValid(data)) { this.viewContainer.createEmbeddedView(this.templateRef); } else {

jQuery trigger('click') not working with Jasmine-jquery

旧街凉风 提交于 2019-12-17 20:29:32
问题 This is my test code: describe("Login", function(){ beforeEach(function(){ loadFixtures('login-fixture.html'); }) it("should enable the button when checking 'remember password'", function(){ $('#remember').trigger('click'); expect($('#keepIn')).not.toBeDisabled(); }); }); And this is my production code: $(document).ready(function(){ $('#remember').click(function(e) { if($('#remember').is(':checked')) { $('#keepIn').removeAttr('disabled'); } }); }); This is not working, the production code

Spying on a constructor using Jasmine

浪子不回头ぞ 提交于 2019-12-17 16:43:06
问题 I am using Jasmine to test if certain objects are created and methods are called on them. I have a jQuery widget that creates flipcounter objects and calls the setValue method on them. The code for flipcounter is here: https://bitbucket.org/cnanney/apple-style-flip-counter/src/13fd00129a41/js/flipcounter.js The flipcounters are created using: var myFlipCounter = new flipCounter("counter", {inc: 23, pace: 500}); I want to test that the flipcounters are created and the setValue method is called