summaryrefslogtreecommitdiffstats
path: root/test/specHelper.js
blob: 4bdd318db35a93f314dc480a314dd85564be0f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
fixture.setBase("test/fixtures");

// finds DOM elements in tests
function $ (str, context) {
	return (context || fixture.el).querySelector(str);
}

function $$ (str, context) {
	return (context || fixture.el).querySelectorAll(str);
}

// bundled fixture load/cleanup
$.fixture = function (fixtureName) {
	beforeEach(function () {
		// Awesomplete probably needs to cleanup this by itself
		try { Awesomplete.all = []; } catch(e) {};
		fixture.load(fixtureName + ".html");
	});

	afterEach(function () {
		fixture.cleanup();
	});
};

// spy to check if event was fired or not
$.spyOnEvent = function (target, type) {
	var handler = jasmine.createSpy(type);
	$.on(target, type, handler);
	return handler;
};

$.on = function (target, type, callback) {
	target.addEventListener(type, callback);
};

$.fire = function (target, type, properties) {
	var evt = document.createEvent("HTMLEvents");
	evt.initEvent(type, true, true );
	for (var j in properties) {
		evt[j] = properties[j];
	}
	target.dispatchEvent(evt);
	return evt;
};

// simulates text input (very simple, only "input" event is fired)
$.type = function (input, text) {
	input.focus();
	input.value = text;
	return $.fire(input, "input");
};

// simulates keydown events
$.keydown = function (target, keyCode) {
	return $.fire(target, "keydown", { keyCode: keyCode });
};
$.k = {
	ENTER: 13,
	ESC:   27,
	DOWN:  40,
	UP:    38
};

// $.noop returns a new empty function each time it's being called
Object.defineProperty($, "noop", {
	get: function () {
		return function noop () {}
	}
});