discourse/spec/controllers/admin/emojis_controller_spec.rb

81 lines
2.1 KiB
Ruby
Raw Normal View History

2014-12-23 08:12:26 +08:00
require 'spec_helper'
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
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) }
context '.index' do
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-01-10 01:04:02 +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
context '.create' do
2014-12-26 00:58:15 +08:00
before { Emoji.expects(:custom).returns([custom_emoji]) }
2014-12-23 08:12:26 +08:00
context 'name already exist' do
it "throws an error" do
xhr :post, :create, { name: "hello", file: "" }
2015-01-10 01:04:02 +08:00
expect(response).not_to be_success
2014-12-23 08:12:26 +08:00
end
end
context 'error while saving emoji' do
it "throws an error" do
Emoji.expects(:create_for).returns(nil)
xhr :post, :create, { name: "garbage", file: "" }
2015-01-10 01:04:02 +08:00
expect(response).not_to be_success
2014-12-23 08:12:26 +08:00
end
end
context 'it works' do
let(:custom_emoji2) do
Emoji.new("/path/to/hello2").tap do |e|
e.name = "hello2"
e.url = "/url/to/hello2.png"
end
end
it "creates a custom emoji" do
Emoji.expects(:create_for).returns(custom_emoji2)
xhr :post, :create, { name: "hello2", file: ""}
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-01-10 01:04:02 +08:00
expect(json['name']).to eq(custom_emoji2.name)
expect(json['url']).to eq(custom_emoji2.url)
2014-12-23 08:12:26 +08:00
end
end
end
context '.destroy' do
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