React测试Mixin

1.test.jsx

 1 var randomNumberMixin = require("./randomNumberMixin.jsx");
 2 
 3 describe("test randomNumberMixin", function () {
 4     it("should return a random number", function () {
 5         expect(randomNumberMixin.randomNumber()).toBeLessThan(1);
 6     });
 7 
 8     it("should return many numbers", function () {
 9         expect(randomNumberMixin.randomNumbers(6).length).toBe(6);
10     })
11 })

2.randomNumberMixin.jsx

 1 var randomNumberMixin = {
 2     randomNumber: function () {
 3         return Math.random();
 4     },
 5     randomNumbers: function (count) {
 6         var result = [];
 7         for (var i = 0; i < count; i++) {
 8             result.push(this.randomNumber);
 9         };
10         return result;
11     }
12 }
13 
14 module.exports = randomNumberMixin;

3.browserify -t reactify test.js > app.js后

app.js

 1 (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
 2 var randomNumberMixin = {
 3     randomNumber: function () {
 4         return Math.random();
 5     },
 6     randomNumbers: function (count) {
 7         var result = [];
 8         for (var i = 0; i < count; i++) {
 9             result.push(this.randomNumber);
10         };
11         return result;
12     }
13 }
14 
15 module.exports = randomNumberMixin;
16 
17 },{}],2:[function(require,module,exports){
18 var randomNumberMixin = require("./randomNumberMixin.jsx");
19 
20 describe("test randomNumberMixin", function () {
21     it("should return a random number", function () {
22         expect(randomNumberMixin.randomNumber()).toBeLessThan(1);
23     });
24 
25     it("should return many numbers", function () {
26         expect(randomNumberMixin.randomNumbers(6).length).toBe(6);
27     })
28 })
29 
30 },{"./randomNumberMixin.jsx":1}]},{},[2]);
原文地址:https://www.cnblogs.com/shamgod/p/5074734.html