Skip to content

Commit 138d228

Browse files
committed
feat: component test, close #11
1 parent 501ae20 commit 138d228

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,16 @@ it('shows hello', () => {
7070

7171
* Testing a value in [value-spec.js](cypress/integration/value-spec.js)
7272
* [filter](cypress/src/reverse.js) and [filter-spec.js](cypress/integration/filter-spec.js)
73+
* [component](cypress/src/hero-detail.js) and [component-spec.js](cypress/integration/component-spec.js)
7374

7475
## Notes
7576

7677
* `npm run cy` opens Cypress end-to-end test runner in GUI mode
7778
* `npm test` runs Cypress in headless mode
7879
* `mount` uses [`angular.bootstrap`](https://docs.angularjs.org/api/ng/function/angular.bootstrap) to mount code inside the test iframe.
7980

81+
Cypress can do everything [you can do from DevTools console](https://glebbahmutov.com/blog/angular-from-browser-console/) when dealing with Angular.js application.
82+
8083
### Small print
8184

8285
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2018

cypress/integration/component-spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/// <reference types="cypress" />
2+
import { mount } from '../..'
3+
import angular from 'angular'
4+
import '../src/hero-detail'
5+
6+
// component example taken from
7+
// https://docs.angularjs.org/guide/component
8+
9+
/* eslint-env mocha */
10+
describe('Component', () => {
11+
const template = `
12+
<!-- components match only elements -->
13+
<div ng-controller="MainCtrl as ctrl">
14+
<b>Hero</b><br>
15+
<hero-detail hero="ctrl.hero"></hero-detail>
16+
</div>
17+
`
18+
19+
it('shows passed name', () => {
20+
// note that module "heroApp" should already exist (in src/hero-detail)
21+
angular.module('heroApp').controller('MainCtrl', function MainCtrl () {
22+
this.hero = {
23+
name: 'Spawn'
24+
}
25+
})
26+
27+
mount(template, ['heroApp'])
28+
cy.contains('Name: Spawn')
29+
})
30+
31+
it('renders when data changes', () => {
32+
angular.module('heroApp').controller('MainCtrl', function MainCtrl () {
33+
this.hero = {
34+
name: 'Spawn'
35+
}
36+
})
37+
38+
mount(template, ['heroApp'])
39+
cy.contains('Name: Spawn').then($el => {
40+
const ngElement = angular.element($el)
41+
ngElement.controller().hero.name = 'Batman'
42+
ngElement.scope().$apply()
43+
})
44+
cy.contains('Name: Batman')
45+
})
46+
})

cypress/src/hero-detail.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import angular from 'angular'
2+
3+
// from https://docs.angularjs.org/guide/component
4+
angular.module('heroApp', []).component('heroDetail', {
5+
template: '<span>Name: {{$ctrl.hero.name}}</span>',
6+
bindings: {
7+
hero: '='
8+
}
9+
})

0 commit comments

Comments
 (0)