Skip to content

Instantly share code, notes, and snippets.

@JackLaBarba
Created July 25, 2014 15:37
Show Gist options
  • Save JackLaBarba/65fc20797de43a4ef950 to your computer and use it in GitHub Desktop.
Save JackLaBarba/65fc20797de43a4ef950 to your computer and use it in GitHub Desktop.
diff --git a/.gitignore b/.gitignore
index 1b6af0b..d615ede 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,4 +20,3 @@ spec/fixtures/*.yml
config/database.yml
.DS_Store
*.swp
-test/fixtures/
diff --git a/config/routes.rb b/config/routes.rb
index 3908199..0c798db 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,9 +1,7 @@
Rails.application.routes.draw do
- defaults format: "json" do
- resources :journals, defaults: {format: :json}
- resources :sections, defaults: {format: :json}, only: %i[create update destroy]
- resources :pages, defaults: {format: :json}
- end
+ resources :journals
+ resources :sections, only: %i[create update destroy]
+ resources :pages
root :to => 'assets#index'
get 'styleguide' => 'styleguide#index' if Rails.env.development?
end
diff --git a/spec/controllers/js_fixtures/pages_fixtures_spec.rb b/spec/controllers/js_fixtures/pages_fixtures_spec.rb
deleted file mode 100644
index e408572..0000000
--- a/spec/controllers/js_fixtures/pages_fixtures_spec.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-require 'rails_helper'
-
-describe PagesController, :js_fixture do
- specify "generation of the pages JS Fixture" do
- journals = create_list(:journal, 3)
- pages = create_list(:page_with_sections, 3)
- pages.should be_present
- pages.each do |page|
- page.should be_persisted
- page.sections.should be_present
- end
-
- get :index
-
- response.should be_success
- save_json_fixture "pages.json", response.body
- save_json_fixture "journals.json", journals: ActiveModel::ArraySerializer.new(journals, each_serializer: JournalSerializer).as_json
-
- get :show, id: pages.first.to_param
- response.should be_success
- save_json_fixture "page.json", response.body
- end
-end
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index 3a7ff6b..58b332c 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -24,7 +24,6 @@ RSpec.configure do |config|
config.include GenericHelpers
config.include FeaturesHelper, type: :feature
- config.include JsFixtures, type: :controller
config.include FactoryGirl::Syntax::Methods
config.before :each do
diff --git a/spec/support/js_fixtures.rb b/spec/support/js_fixtures.rb
deleted file mode 100644
index d332d55..0000000
--- a/spec/support/js_fixtures.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-module JsFixtures
-
- FIXTURE_DIR = Rails.root.join("test", "fixtures")
-
- def self.clear!
- files = Dir[FIXTURE_DIR.join("*.{html,json}")]
- files.reject! { |file| File.basename(file).starts_with?("_") }
- FileUtils.rm files
- end
-
- def save_fixture(name, content)
- filename = File.join(FIXTURE_DIR, name)
- sanitized_content = sanitize_html_of_scripts content
- File.open(filename, "w+") do |f|
- f.puts sanitized_content
- end
- content
- end
-
- def save_json_fixture(name, content)
- content = content.to_json unless content.kind_of? String
- JSON.parse(content) # Check that it's valid
- filename = File.join(FIXTURE_DIR, name)
- File.open(filename, "w+") do |f|
- f.puts content
- end
- content
- end
-
- def sanitize_html_of_scripts(html)
- doc = Nokogiri::HTML.fragment(html)
- doc.css("script").each(&:remove)
- doc.to_html
- end
-
-end
diff --git a/test/javascripts/controllers/page_controller_test.js.coffee b/test/javascripts/controllers/page_controller_test.js.coffee
index 088dd8e..fb7b8d1 100644
--- a/test/javascripts/controllers/page_controller_test.js.coffee
+++ b/test/javascripts/controllers/page_controller_test.js.coffee
@@ -9,48 +9,69 @@ moduleFor "controller:page", "PageController",
setup: ->
@store = Lemur.__container__.lookup("store:main")
-
- pretend ->
- @get '/journals', -> [200, {"Content-Type": "application/json"}, readFixture("journals")]
- @get '/pages/:id', -> [200, {"Content-Type": "application/json"}, readFixture("page")]
-
- @journals = @store.find "journal"
-
- stop()
- Ember.RSVP.all([@journals, @page]).then =>
- @page = @store.find("page", 1)
- start()
-
+ Ember.run =>
+ @journals = (@store.createRecord("journal", id: i+1, name: name) for name, i in ["foo", "bar", "qaz"])
+ @sections = [
+ @store.createRecord("section", id: 1, page_id: 1, body: "First Global"),
+ @store.createRecord("section", id: 2, page_id: 1, body: "Second Global"),
+ @store.createRecord("section", id: 3, page_id: 1, body: "Non Global")
+ ]
+ @page = @store.createRecord("page", id: 1, name: "Page", audience: "public_audience")
+
+ @page.get("sections").addObject(section) for section in @sections
+ for journal in @journals
+ @sections[0].get("journals").addObject(journal)
+ @sections[1].get("journals").addObject(journal)
+
+ @sections[2].get("journals").addObject(journal) for journal in @journals[0..1]
+
+# setup: ->
+# @store = Lemur.__container__.lookup("store:main")
+# journals = ({id: i+1, name: name} for name, i in ["foo", "bar", "qaz"])
+# pretend ->
+# @get '/journals', ->
+# [200, {"Content-Type": "application/json"}, JSON.stringify(journal: journals)]
+# @get '/pages', ->
+# response = [
+# page: {id: 1, name: "Page", audience: "public_audience", section_ids: [1, 2, 3]},
+# section: [
+# {id: 1, page_id: 1, journal_ids: journals.mapBy("id"), body: "First Global"},
+# {id: 2, page_id: 1, journal_ids: journals.mapBy("id"), body: "Second Global"},
+# {id: 3, page_id: 1, journal_ids: journals.mapBy("id")[0..1], body: "Non Global"}
+# ]
+# ]
+# [200, {"Content-Type": "application/json"}, JSON.stringify(response)]
test "it calculates the number of global sections", ->
expect(6)
- diff = (a, b) ->
- klass = Ember.Object.extend differ: Ember.computed.setDiff("a", "b")
- klass.create(a: a, b: b).get("differ").toArray()
-
- equal @page.get('id'), 1
-
pageController = @subject(model: @page)
+ debugger;
+ page = null
+ Ember.run => page = @store.find("page", 1)
+
Ember.run =>
- pageController.set "model", @page
+ pageController.set "model", page
pageController.get("controllers.journals").set("content", @journals)
- Ember.run => equal pageController.get("model.sections.length"), 4
+ Ember.run => equal pageController.get("model.sections.length"), 3
- Ember.run =>
- equal(pageController.get("globalSections.length"), 1)
- equal(pageController.get("nonGlobalSections.length"), 3)
+ properties = (pageController.get(prop) for prop in "model journals sections".w())
+ Ember.RSVP.all(properties).then ([page, journals, sections]) =>
+ equal(pageController.get("globalSections.length"), sections.get("length") - 1)
- nonGlobal = pageController.get("nonGlobalSections").objectAt(0)
+ nonGlobal = sections.objectAt(2)
+ journal3 = journals.objectAt(2)
- missingJournals = diff(@journals.toArray(), nonGlobal.get('journals'))
+ equal(nonGlobal.get("id"), 3)
+ equal(journal3.get("id"), 3)
- Ember.run =>
- nonGlobal.get("journals").addObject(missingJournal) for missingJournal in missingJournals
- nonGlobal.notifyPropertyChange("journals")
+ ok !nonGlobal.get("journals").mapBy("id").contains(journal3.get("id"))
+
+ Ember.run =>
+ nonGlobal.get("journals").addObject(journal3)
+ nonGlobal.notifyPropertyChange("journals")
- equal(pageController.get("globalSections.length"), 2)
- equal(pageController.get("nonGlobalSections.length"), 2)
+ equal(pageController.get("globalSections.length"), sections.get("length"))
diff --git a/test/javascripts/support/js_fixtures.js.coffee.erb b/test/javascripts/support/js_fixtures.js.coffee.erb
deleted file mode 100644
index fe96395..0000000
--- a/test/javascripts/support/js_fixtures.js.coffee.erb
+++ /dev/null
@@ -1,9 +0,0 @@
-window.JS_FIXTURES = {
-<% Dir[Rails.root.join("test", "fixtures", "*.json")].map do |file| %>
- <%= File.basename(file, ".json") %>: <%= File.read(file).to_json %>
-<% end %>
-}
-
-window.readFixture = (fixtureName) ->
- throw("CANNOT FIND FIXTURE #{fixtureName}") if !(fixtureName of JS_FIXTURES)
- JS_FIXTURES[fixtureName]
diff --git a/test/javascripts/test_helper.js.coffee b/test/javascripts/test_helper.js.coffee
index d4d4626..f1cd82e 100644
--- a/test/javascripts/test_helper.js.coffee
+++ b/test/javascripts/test_helper.js.coffee
@@ -2,7 +2,6 @@
#= require ./ember_qunit_helper
#= require pretender_with_dependencies
#= require sinonjs/sinon
-#= require_tree ./support
#= require_tree .
#= require_self
@@ -40,20 +39,7 @@ window.assertNoPageSelector = (selector) ->
elements = find selector
ok elements.length is 0, "Expected page NOT to contain selector \"#{selector}\""
-window.pretend = (callback) ->
- window._pretenderServer = server = new Pretender(callback)
-
- server.handledRequest = (verb, path, request) ->
- # console.debug("PRETENDER HANDLING #{verb} #{path}", request)
-
- server.erroredRequest = (verb, path, request, error) ->
- console.error("PRETENDER ERROR!", verb, path, request, error)
-
- server.unhandledRequest = (verb, path, request) ->
- console.log("PRETENDER UNHANDLED #{verb} REQUEST TO #{path}", request)
-
- server
-
+window.pretend = (callback) -> window._pretenderServer = new Pretender(callback)
QUnit.testDone ->
window._pretenderServer?.shutdown()
delete window._pretenderServer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment