2015-10-11 17:41:23 +08:00
|
|
|
require "rails_helper"
|
2014-12-23 08:12:26 +08:00
|
|
|
|
|
|
|
describe Admin::EmojisController do
|
|
|
|
|
|
|
|
let(:custom_emoji) do
|
|
|
|
Emoji.new("/path/to/hello").tap do |e|
|
|
|
|
e.name = "hello"
|
|
|
|
e.url = "/url/to/hello.png"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
let(:custom_emoji2) do
|
|
|
|
Emoji.new("/path/to/hello2").tap do |e|
|
|
|
|
e.name = "hello2"
|
|
|
|
e.url = "/url/to/hello2.png"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-23 08:12:26 +08:00
|
|
|
it "is a subclass of AdminController" do
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(Admin::EmojisController < Admin::AdminController).to eq(true)
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when logged in" do
|
|
|
|
let!(:user) { log_in(:admin) }
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
context ".index" do
|
2014-12-23 08:12:26 +08:00
|
|
|
it "returns a list of custom emojis" do
|
|
|
|
Emoji.expects(:custom).returns([custom_emoji])
|
|
|
|
xhr :get, :index
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-12-23 08:12:26 +08:00
|
|
|
json = ::JSON.parse(response.body)
|
2015-05-20 22:45:31 +08:00
|
|
|
expect(json[0]["name"]).to eq(custom_emoji.name)
|
|
|
|
expect(json[0]["url"]).to eq(custom_emoji.url)
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
context ".create" do
|
2014-12-23 08:12:26 +08:00
|
|
|
|
2014-12-26 00:58:15 +08:00
|
|
|
before { Emoji.expects(:custom).returns([custom_emoji]) }
|
2014-12-23 08:12:26 +08:00
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
context "name already exist" do
|
2014-12-23 08:12:26 +08:00
|
|
|
it "throws an error" do
|
2015-05-20 22:45:31 +08:00
|
|
|
message = MessageBus.track_publish do
|
|
|
|
xhr :post, :create, { name: "hello", file: "" }
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(message.data["errors"]).to be
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
context "error while saving emoji" do
|
2014-12-23 08:12:26 +08:00
|
|
|
it "throws an error" do
|
|
|
|
Emoji.expects(:create_for).returns(nil)
|
2015-05-20 22:45:31 +08:00
|
|
|
message = MessageBus.track_publish do
|
|
|
|
xhr :post, :create, { name: "garbage", file: "" }
|
|
|
|
end.first
|
|
|
|
|
|
|
|
expect(response).to be_success
|
|
|
|
expect(message.data["errors"]).to be
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
it "works" do
|
|
|
|
Emoji.expects(:create_for).returns(custom_emoji2)
|
2014-12-23 08:12:26 +08:00
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
message = MessageBus.track_publish do
|
2014-12-23 08:12:26 +08:00
|
|
|
xhr :post, :create, { name: "hello2", file: ""}
|
2015-05-20 22:45:31 +08:00
|
|
|
end.first
|
2014-12-23 08:12:26 +08:00
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
expect(response).to be_success
|
|
|
|
|
|
|
|
expect(message.data["name"]).to eq(custom_emoji2.name)
|
|
|
|
expect(message.data["url"]).to eq(custom_emoji2.url)
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-20 22:45:31 +08:00
|
|
|
context ".destroy" do
|
2014-12-23 08:12:26 +08:00
|
|
|
it "deletes the custom emoji" do
|
|
|
|
custom_emoji.expects(:remove)
|
|
|
|
Emoji.expects(:custom).returns([custom_emoji])
|
|
|
|
xhr :delete, :destroy, id: "hello"
|
2015-01-10 01:04:02 +08:00
|
|
|
expect(response).to be_success
|
2014-12-23 08:12:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|