Skip to content

Commit 5d94074

Browse files
committed
Ensure global namespace is not polluted when using requireJS. And add qunit test of basics when included using requireJS
1 parent e909913 commit 5d94074

File tree

3 files changed

+79
-2
lines changed

3 files changed

+79
-2
lines changed

state-machine.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(function (window) {
22

3-
StateMachine = {
3+
var StateMachine = {
44

55
//---------------------------------------------------------------------------
66

@@ -145,7 +145,7 @@
145145
//===========================================================================
146146

147147
if ("function" === typeof define) {
148-
define([], function() { return StateMachine; });
148+
define(function(require) { return StateMachine; });
149149
}
150150
else {
151151
window.StateMachine = StateMachine;

test/requirejs/index.html

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Finite State Machine Tests - USING REQUIREJS INCLUDE MECHANISM</title>
5+
<link rel="stylesheet" href="../qunit/qunit.css" media="screen">
6+
<script src="../qunit/qunit.js"></script>
7+
<script src="require.js"></script>
8+
<script>
9+
require(["../../state-machine.js"], function(RequiredStateMachine) {
10+
11+
module("require.js");
12+
13+
test("state machine included using require.js with custom name", function() {
14+
15+
var fsm = RequiredStateMachine.create({
16+
initial: 'green',
17+
events: [
18+
{ name: 'warn', from: 'green', to: 'yellow' },
19+
{ name: 'panic', from: 'yellow', to: 'red' },
20+
{ name: 'calm', from: 'red', to: 'yellow' },
21+
{ name: 'clear', from: 'yellow', to: 'green' }
22+
]});
23+
24+
equals(fsm.current, 'green', "initial state should be green");
25+
26+
fsm.warn(); equals(fsm.current, 'yellow', "warn event should transition from green to yellow");
27+
fsm.panic(); equals(fsm.current, 'red', "panic event should transition from yellow to red");
28+
fsm.calm(); equals(fsm.current, 'yellow', "calm event should transition from red to yellow");
29+
fsm.clear(); equals(fsm.current, 'green', "clear event should transition from yellow to green");
30+
31+
});
32+
33+
});
34+
</script>
35+
</head>
36+
<body class="flora">
37+
<h1 id="qunit-header">QUnit Test Suite</h1>
38+
<h2 id="qunit-banner"></h2>
39+
<div id="qunit-testrunner-toolbar"></div>
40+
<h2 id="qunit-userAgent"></h2>
41+
<ol id="qunit-tests"></ol>
42+
<div id="qunit-fixture">test markup</div>
43+
</body>
44+
</html>

test/requirejs/require.js

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)