Calling a function in a child or sibling component in React

“How to call a function in child or sibling component” is a fairly frequently asked question in React world. This is how we’ve solved it.

The setup is this: we have a parent component and one or two child components. We need to raise an event in a child component either from the parent component, or we need a child component A to raise an event in child component B.

Continue reading “Calling a function in a child or sibling component in React”

How to run Jasmine tests as nUnit tests and test sites you don’t own.

Problem:
We’ve been using a 3rd party tool to generate a page, and I had to test some data there.
Since I had no knowledge of the DOM (it would be preferable to think of it as a black box), I couldn’t really use traditional tools that would validate the presence of certain elements on a page. A better way would be to inject a script into a page and validate the content of some global variables.

In my scenario it also would be preferable to keep test code separate from the site being tested and treat it as something I don’t own (it’s being built by another team, so it would be easier).

So I want to be able to inject my Jasmine tests into a page; I want to see the list of tests as NUnit tests. Continue reading “How to run Jasmine tests as nUnit tests and test sites you don’t own.”

Controller communication in AngularJS

Quite often there is a need to pass data from one controller to another. This is relatively easy if we need to pass data from child to parent controller, but not so straight forward when controllers are not in that relationship.
Still, there’s quite a few ways of achieving that, differing in ease of implementation (though none are hard), performance and ease of maintenance.

Angular offers emit/broadcast/on methods on a scope, which are a lot like events in say C#, but they only broadcasted up and down in scope inheritance, so if you need to share data with sibling or some random controller in your app you need to jump through the hoops. Continue reading “Controller communication in AngularJS”

Data mocking in angular E2E testing

We needed to create a suite of e2e tests for our web app, preferably without having to hit the database for obvious reasons.
So basically I wanted to have a solution that satisfies these requirements:

  • Allows easy switch between real and test data, for example by specifying a URL parameter.
  • Allow specifying a mock data source, and a way to manipulate that mock data after it was loaded from the source.

Angular has a fake $httpBackend implementation designed specifically for providing a mock data response when you’re using $http service. Continue reading “Data mocking in angular E2E testing”

JavaScript testing: quick start

How to quickly setup a JS testing with Karma runner and Jasmine:

Install node (http://nodejs.org/download/)
Open the command prompt, and run
npm install -g karma
//Packages are installed to c:\Users\your-user-name\AppData\Roaming
pm
Generate karma config file.
In a command prompt: cd to your development folder, and

karma init

If you get a warning

WARN [init]: No binary for Chrome. Create symlink at "C:\Users\user-name\AppDataLocal\Google\Chrome\Application\chrome.exe", or set "CHROME_BIN" env variable.

then run this command: set CHROME_BIN=C:/Program Files (x86)/Google/Chrome/Application/chrome.exe

Open karma.conf.js, and set files array to files = [JASMINE, JASMINE_ADAPTER, ‘fileWithTests.js’];, where fileWithTest is a test container.

To write tests consult Jasmine help page: http://pivotal.github.io/jasmine/

describe("A suite", function() {
  it("contains spec with an expectation", function() {
    expect(true).toBe(true);
  });
});

Done.