Question

I have a model called Url and I have a counter in the root page, I update the counter value with javascript (actually coffescript) using pusher.

In my root page I have a javascript that subscribe to a chanel and wait for a message:

pusher = new Pusher(gon.pusher_key)
channel = pusher.subscribe("url_counter")
channel.bind "update_url_counter", (data) ->
  $(counter).text(data.urlsCount)

Then when I create or delete an url I send a message to pusher with the counter value:

Pusher.trigger("url_counter", "update_url_counter", {urlsCount: Url.all.count})

The object in the view which contains the counter is this:

Total urls: <span id="url_counter"><%=@url_count%></span>

The thing works fine. But I don't know how to test it.

I hope the information is good enough and somebody can help me.

Thanks in advance.

Was it helpful?

Solution

This is the way I tested it.

On one hand I made sure when a url is created Pusher is called correctly

url = "http://test.com"

expect(Pusher).to receive(:trigger).with("urlmini", "update_url_counter", {urlsCount: 1}).and_return {}

visit new_url_path
fill_in "url_text", with: original
click_button "submit_url"

Then to test if my JavaScript is working correctly I call Pusher

visit root_path
within "#url_counter" do
  expect(page).to have_content "0"
end
fake_counter = 10
Pusher.trigger("urlmini", "update_url_counter", {urlsCount: fake_counter})
within "#url_counter" do
  expect(page).to have_content fake_counter
end 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top