Skip to content

Instantly share code, notes, and snippets.

@willmanduffy
Created February 25, 2016 22:46
Show Gist options
  • Select an option

  • Save willmanduffy/f638d3b7e07b584edda7 to your computer and use it in GitHub Desktop.

Select an option

Save willmanduffy/f638d3b7e07b584edda7 to your computer and use it in GitHub Desktop.
Cool test tho
class EmojiSearcherTest < ActiveSupport::TestCase
let(:collection) { create(:collection) }
let(:user) { create(:user) }
describe '#search' do
before { populate_emoji_list }
describe 'when a query argument is sent' do
let(:params) do
{
query: 'batman'
}
end
subject { EmojiSearcher.new(params: params) }
it 'should only return emoji associated with batman' do
expected_result = [
@batman_name_emoji,
@batman_shortcut_emoji
]
subject.search.order(:created_at).must_equal expected_result
end
end
describe 'when a collection_id argument is sent' do
let(:params) do
{
collection_id: collection.id
}
end
before { collection.emojis << @batman_shortcut_emoji }
subject { EmojiSearcher.new(params: params) }
it 'must only return emoji in the collection' do
expected_result = [
@batman_shortcut_emoji
]
subject.search.order(:created_at ).must_equal expected_result
end
end
describe 'when current_user is sent' do
let(:params) do
{
current_user: true
}
end
subject { EmojiSearcher.new(params: params, user: user) }
it 'must only return emoji that belong to the user' do
expected_result = [
@batman_shortcut_emoji
]
subject.search.order(:created_at).must_equal expected_result
end
end
describe 'when all arguments are sent together' do
let(:params) do
{
query: 'batman',
collection_id: collection.id,
current_user: true
}
end
before do
collection.emojis << [
@batman_name_emoji,
@batman_shortcut_emoji,
@robin_name_emoji
]
end
subject { EmojiSearcher.new(params: params, user: user) }
it 'must only return matching emoji' do
expected_result = [
@batman_shortcut_emoji
]
subject.search.order(:created_at).must_equal expected_result
end
end
describe 'when no params are sent' do
subject { EmojiSearcher.new(params: nil) }
it 'should return no emoji' do
subject.search.must_equal []
end
end
end
private
def populate_emoji_list
@emojis = [
@batman_name_emoji = create(:emoji, name: 'Lonely Batman'),
@batman_shortcut_emoji = create(:emoji, shortcut: 'batman', user: user),
@robin_name_emoji = create(:emoji, name: 'Slanderous robin'),
@robin_shortcut_emoji = create(:emoji, shortcut: 'robin')
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment