Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Unit Tests #1

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
308e4ce
For WebStorm users.
Apr 24, 2013
45d4ba6
Initial descriptor for NPM (used primarily to start tests).
Apr 24, 2013
6c45b42
Test configuration.
Apr 24, 2013
145008b
I hate semis.
Apr 24, 2013
c9aa8c1
Notes on running tests.
Apr 24, 2013
b2d9f6a
Travis CI configuration.
Apr 24, 2013
d05c785
Include PhantomJS as a devDependency.
Apr 24, 2013
a66f941
Single run by default.
Apr 24, 2013
4383469
First cut.
Apr 24, 2013
9d4acdb
Merge branch 'issue-1' of github.com:michaelahlers/angular-timezone-j…
Apr 24, 2013
411b29f
Link to Travis.
Apr 24, 2013
580d3ab
We're not concerned with multiple Node releases.
Apr 24, 2013
1918dfe
Not necessary per http://about.travis-ci.org/docs/user/gui-and-headle…
Apr 24, 2013
3608487
A valid test, but failing.
Apr 24, 2013
542d2b8
TimezoneJS is required.
Apr 24, 2013
592b75d
Inject $filter for future test cases.
Apr 24, 2013
ac24600
Tweaks. Serve the timezones.
Apr 25, 2013
6d9f4c3
Use provided constant if available.
Apr 25, 2013
5b8f3fd
Make this truly optional.
Apr 25, 2013
9119785
Real test.
Apr 25, 2013
ee6ac81
Use another zone.
Apr 25, 2013
58aea76
Test for formatted dates.
Apr 25, 2013
13d1302
For now, this test fails. The next step is to fix bugs.
Apr 25, 2013
d4076be
Add in hours and the TZ offset.
Apr 25, 2013
d7fef5a
Wrap to prevent leaking variables to the global scope.
Apr 25, 2013
8dd2a17
Extend a native Date object to trick AngularJS's date filter into for…
Apr 25, 2013
369f8b9
Note to self.
Apr 25, 2013
b124645
Angular now tricked into accepting our date objects.
Apr 25, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
bower_components
node_modules
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js

node_js:
- "0.10"

script:
- npm test
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
TimezoneJS for AngularJS
========================
# TimezoneJS for AngularJS [![Build Status](https://secure.travis-ci.org/michaelahlers/angular-timezone-js.png)](http://travis-ci.org/michaelahlers/angular-timezone-js)

You need:

- npm
- bower

TODO
----
- NPM
- Bower

- Angular Date detection prevents using date filter for TimezoneJS instances – now using patched version of Angular
## Developers

_TimezoneJS for Angular_ is tested with [Karma](http://karma-runner.github.io/) and [PhantomJS](http://phantomjs.org/) against the latest available release of [jQuery](http://jquery.com/) (1.9.1) and [AngularJS](http://angularjs.com/) (1.1.4).

```
$ npm install --dev
$ npm test
```
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
<body>
<h1>Angular.js TimezoneJS demo</h1>

<p>Hey folks! Today is <span>{{ today | tzDate:timezone | date:"yyyy-MM-dd" }}</span></p>
<p>Hey folks! Today is <span>{{ today | tzDate:timezone | date:"yyyy-MM-dd HH:mm:ss Z" }}</span></p>
</body>
</html>
3 changes: 2 additions & 1 deletion js/bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"dependencies": {
"angular": "latest",
"jquery": "latest",
"angular-filters": "latest"
"angular-filters": "latest",
"timezone-js": "[email protected]:mde/timezone-js.git"
}
}
28 changes: 24 additions & 4 deletions js/timezone.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
(function(){

var toExtendedNative = function (wrapped) {
/* Tricks the isDate method in Angular into treating these objects like it
* would any other Date. May be horribly slow. */
var native = new Date()
for (key in wrapped) {
native[key] = wrapped[key]
}
return native
}

var timezonejs = angular.module('timezonejs', []);

timezonejs.factory('Timezone', function() {
timezonejs.factory('Timezone', function($injector) {
var _tz = timezoneJS.timezone;
_tz.loadingScheme = _tz.loadingSchemes.MANUAL_LOAD;
// TODO: load with ang?
_tz.loadZoneJSONData('js/lib/timezones.json', true);

try {
_tz.loadZoneJSONData($injector.get('timezonesURL'), true);
} catch (e) {
_tz.loadZoneJSONData('js/lib/timezones.json', true);
}

timezoneJS.fromLocalString = function(str, tz) {
// https://github.com/csnover/js-iso8601/blob/master/iso8601.js – MIT license

Expand All @@ -20,14 +38,16 @@ timezonejs.factory('Timezone', function() {
struct[2] = (+struct[2] || 1) - 1;
struct[3] = +struct[3] || 1;

return new timezoneJS.Date(struct[1], struct[2], struct[3], struct[4], struct[5], struct[6], struct[7], tz);
return toExtendedNative(new timezoneJS.Date(struct[1], struct[2], struct[3], struct[4], struct[5], struct[6], struct[7], tz));
};
return timezoneJS;
});

timezonejs.filter('tzDate', function(Timezone) {
return function(dt, tz) {
console.log('ar', arguments);
return new Timezone.Date(dt, tz);
return toExtendedNative(new Timezone.Date(dt, tz));
};
});

})(angular)
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name" : "angular-timezone-js",
"description" : "TimezoneJS for AngularJS",
"version" : "0.1.0",
"homepage" : "https://github.com/LeZuse/angular-timezone-js",
"author" : "Tomas Ruzicka <[email protected]>",

"repository" : {
"type" : "git",
"url" : "[email protected]:LeZuse/angular-timezone-js.git"
},

"engines" : {
"node" : ">= 0.8"
},

"dependencies" : {},

"devDependencies" : {
"karma" : "0.8.x",
"phantomjs" : "1.9.x"
},

"scripts" : {
"test" : "karma start test/karma.conf.js"
},

"contributors" : [
{
"name" : "Michael Ahlers",
"email" : "[email protected]"
}
]
}
92 changes: 92 additions & 0 deletions test/directives/tzDateSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict'

describe('tzDate', function () {
var scope, $filter, $compile, $timeout, $sandbox

beforeEach(module('timezonejs', function ($provide) {
$provide.value('timezonesURL', 'base/js/lib/timezones.json')
}))

beforeEach(inject(function ($injector, $rootScope, _$filter_, _$compile_, _$timeout_) {
scope = $rootScope
$filter = _$filter_
$compile = _$compile_
$timeout = _$timeout_

$sandbox = $('<div id="sandbox"></div>').appendTo($('body'))
}))

var compile = function (scenario) {
angular.extend(scope, scenario.scope)

var $element = $(scenario.markup).appendTo($sandbox)

$element = $compile($element)(scope)
scope.$digest()

return $element
}

afterEach(function () {
$sandbox.remove()
scope.$destroy()
})

var scenarios = [
{
scope : {
reference : new Date(Date.parse('1970-01-01T00:00:00+00:00')),
timezone : 'America/New_York'
},
markup : '<span>{{reference|tzDate:timezone|date:"yyyy-MM-dd HH:mm:ss Z"}}</span>',
expected : {
fullYear : 1969,
month : 11,
date : 31,
hours : 20,
text : '1969-12-31 20:00:00 -0400'
}
},
{
scope : {
reference : new Date(Date.parse('1970-01-01T00:00:00+00:00')),
timezone : 'America/Los_Angeles'
},
markup : '<span>{{reference|tzDate:timezone|date:"yyyy-MM-dd HH:mm:ss Z"}}</span>',
expected : {
fullYear : 1969,
month : 11,
date : 31,
hours : 17,
text : '1969-12-31 17:00:00 -0700'
}
}
]

it('should align dates to expected timezones', function () {
scenarios.forEach(function (scenario) {
var timezone = scenario.scope.timezone
, reference = scenario.scope.reference
, expected = scenario.expected

var aligned = $filter('tzDate')(reference, timezone)

expect(aligned.getTimezone()).toEqual(timezone)

expect(aligned.getFullYear()).toEqual(expected.fullYear)
expect(aligned.getMonth()).toEqual(expected.month)
expect(aligned.getDate()).toEqual(expected.date)
expect(aligned.getHours()).toEqual(expected.hours)
})
})

it('should support formatting dates to expected timezones', function () {
scenarios.forEach(function (scenario) {
var expected = scenario.expected
, el = compile(scenario)

expect(el.text()).toEqual(expected.text)
})
})

})
71 changes: 71 additions & 0 deletions test/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Testacular configuration. Visit https://github.com/vojtajina/testacular/blob/stable/lib/config.js#L54
* for all available config options and defaults.
*/

/* Base path, that will be used to resolve files and exclude. */
basePath = './..'

/* Files and patterns to load in the browser. */
files = [
JASMINE,
JASMINE_ADAPTER,

'http://code.jquery.com/jquery-1.9.1.js',
'http://code.angularjs.org/1.1.4/angular.js',
'http://code.angularjs.org/1.1.4/angular-mocks.js',
'https://raw.github.com/mde/timezone-js/master/src/date.js',

{ pattern : 'js/lib/timezones.json', included : false },
'js/timezone.js',

'test/directives/*Spec.js'
]

/* Files to exclude. */
exclude = []

/* CLI progress reporters. Use dots, Travis terminal does not support escaping
* sequences. Legal values: 'dots', 'progress', 'junit'. */
reporters = ['progress']

/* web server port */
// --port 9876
port = 9876

/* CLI runner port. */
// --runner-port 9100
runnerPort = 9100

/* Enable colors in the output (reporters and logs). */
// --colors
// --no-colors
colors = true

/* Logging level. Possible values: LOG_DISABLE, LOG_ERROR, LOG_WARN, LOG_INFO,
* LOG_DEBUG. */
// --log-level debug
logLevel = LOG_INFO

/* Enable watching file and executing tests whenever any file changes. */
// --auto-watch
// --no-auto-watch
autoWatch = true

/* Testing environment. Options are (as available) Chrome, ChromeCanary,
* Firefox, Opera, Safari, PhantomJS, and IE. */
// --browsers Chrome,Firefox,Safari
browsers = ['PhantomJS']

/* Timeout (in milliseconds) for browser capture. */
// --capture-timeout 5000
captureTimeout = 5000

/* Run tests on start (when browsers are captured) and exit. */
// --single-run
// --no-single-run
singleRun = true

/* Report which specs are take longer than 500 ms to complete. */
// --report-slower-than 500
reportSlowerThan = 500