Created
May 28, 2014 17:10
-
-
Save dgeb/154fd68b6d476eadaf11 to your computer and use it in GitHub Desktop.
Ember-Orbit RecordArray filtering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Orbit from 'orbit'; | |
import { RecordNotFoundException, RecordAlreadyExistsException } from 'orbit_common/lib/exceptions'; | |
import attr from 'ember_orbit/attr'; | |
import Context from 'ember_orbit/context'; | |
import Store from 'ember_orbit/store'; | |
import Model from 'ember_orbit/model'; | |
import { createStore } from 'test_helper'; | |
var get = Ember.get, | |
set = Ember.set; | |
var Planet, | |
store, | |
context; | |
module("Unit - Context", { | |
setup: function() { | |
Orbit.Promise = Ember.RSVP.Promise; | |
Planet = Model.extend({ | |
name: attr('string'), | |
atmosphere: attr('boolean'), | |
classification: attr('string') | |
}); | |
store = createStore({ | |
models: { | |
planet: Planet | |
} | |
}); | |
context = Context.create({ | |
store: store | |
}); | |
}, | |
teardown: function() { | |
Orbit.Promise = null; | |
Planet = null; | |
store = null; | |
context = null; | |
} | |
}); | |
test("#filter returns a live RecordArray that stays in sync with filtered records of one type", function() { | |
expect(15); | |
Ember.run(function() { | |
var allPlanets = context.filter('planet'), earth; | |
var terrestrialPlanets = context.filter('planet', function(planet) { | |
return get(planet, 'classification') === 'terrestrial'; | |
}); | |
var atmosphericPlanets = context.filter('planet', function(planet) { | |
return get(planet, 'atmosphere'); | |
}); | |
equal(get(allPlanets, 'length'), 0, 'no records have been added yet'); | |
equal(get(terrestrialPlanets, 'length'), 0, 'no records have been added yet'); | |
equal(get(atmosphericPlanets, 'length'), 0, 'no records have been added yet'); | |
Ember.RSVP.all([ | |
context.add('planet', {name: 'Jupiter', classification: 'gas giant', atmosphere: true}), | |
context.add('planet', {name: 'Venus', classification: 'terrestrial', atmosphere: true}), | |
context.add('planet', {name: 'Mercury', classification: 'terrestrial', atmosphere: false}) | |
]).then(function() { | |
equal(get(allPlanets, 'length'), 3, '3 total planets have been added'); | |
equal(get(terrestrialPlanets, 'length'), 2, '2 terrestrial planets have been added'); | |
equal(get(atmosphericPlanets, 'length'), 2, '2 atmospheric planets have been added'); | |
return context.add('planet', {name: 'Earth', classification: 'terrestrial', atmosphere: true}); | |
}).then(function(earth) { | |
equal(get(allPlanets, 'length'), 4, '4 total planets have been added'); | |
equal(get(terrestrialPlanets, 'length'), 3, '3 terrestrial planets have been added'); | |
equal(get(atmosphericPlanets, 'length'), 3, '3 atmospheric planets have been added'); | |
return context.remove('planet', earth); | |
}).then(function() { | |
equal(get(allPlanets, 'length'), 3, '3 total planets have been added'); | |
equal(get(terrestrialPlanets, 'length'), 2, '2 terrestrial planets have been added'); | |
equal(get(atmosphericPlanets, 'length'), 2, '2 atmospheric planets have been added'); | |
return context.find('planet', {name: 'Jupiter'}); | |
}).then(function(planetsNamedJupiter) { | |
Ember.run(function() { | |
set(planetsNamedJupiter[0], 'classification', 'terrestrial'); // let's just pretend :) | |
}); | |
}).then(function() { | |
equal(get(allPlanets, 'length'), 3, '3 total planets have been added'); | |
equal(get(terrestrialPlanets, 'length'), 3, '3 terrestrial planets have been added'); | |
equal(get(atmosphericPlanets, 'length'), 2, '2 atmospheric planets have been added'); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment