-
Notifications
You must be signed in to change notification settings - Fork 18
/
test.js
99 lines (75 loc) · 2.5 KB
/
test.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* tape tests for the bitcoinaddress.js.
*
* Check that our library loads and works with all browsers using testling.
*
* http://www.catonmat.net/blog/writing-javascript-tests-with-tape/
*/
/* jshint globalstrict:true */
/* globals require, __dirname, window, console */
"use strict";
var test = require("tape");
var $ = require("jquery1-browser");
var bitcoinaddress = require("./bitcoinaddress");
// Load test payload HTML from an external file using brfs complile
// time transformation
// http://stackoverflow.com/a/16951238/315168
var fs = require('fs');
var TEST_HTML = fs.readFileSync(__dirname + '/test-payload.html');
// Dummy BC address used in payload
var TEST_ADDRESS = "xxx";
/**
* Initialize bitcoinaddress
*/
function init() {
// Basic initialization
bitcoinaddress.init({
selector: ".bitcoin-address",
template: "bitcoin-address-template",
jQuery: $
});
}
function reset() {
// Clean document from all HTML content
// (make sure don't clear <script> tags)
$(document.body).children("div").remove();
// Load test HTML snippets
$(document.body).append(TEST_HTML);
}
// Don't execute tests until we have document ready
$(function() {
test("Create Bitcoin address actions", function(t) {
reset();
// Check that the test payload is loaded
t.equal($("#test-address").size(), 1);
t.equal($("#bitcoin-address-template").size(), 1);
// Initialize bitcoinaddress module
init();
// Now #test-address should be transformed
// Check we get 3 actions for handling the address
var actions = $("#test-address .bitcoin-address-action");
t.equal(actions.size(), 3);
// Check address is still displayed correctly
var addr = $("#test-address .bitcoin-address");
t.equal(addr.text(), TEST_ADDRESS);
t.end();
});
test("Show QR code", function(t) {
reset();
init();
// Simulate QR action
var addr = $("#test-address");
var action = addr.find(".bitcoin-address-action-qr");
action.click();
// Let the event sunk in
setTimeout(function() {
var qrContainer = addr.find(".bitcoin-address-qr-container");
var imgs = qrContainer.find("img");
t.equals(imgs.size(), 1);
var src = imgs.attr("src");
t.ok(src, "QR code <img> src was empty");
t.equals(src.slice(0, 5), "data:");
t.end();
}, 2500);
});
});