spec/reducer_spec.rb in rack-reducer-1.1.2 vs spec/reducer_spec.rb in rack-reducer-2.0.0
- old
+ new
@@ -1,9 +1,9 @@
require 'spec_helper'
require_relative 'fixtures'
-RSpec.describe Rack::Reducer do
+RSpec.describe 'Rack::Reducer' do
using SpecRefinements
let(:app) do
lambda do |env|
req = Rack::Request.new(env)
@@ -62,22 +62,36 @@
it 'accepts nil as params' do
expect(Fixtures::ArtistReducer.apply(nil)).to be_truthy
end
- it 'applies default filters' do
- get '/artists' do |response|
- name = response.json[0]['name']
- expect(name).to eq('Björk')
+ describe 'with default filters' do
+ let(:app) do
+ sort = ->(sort: 'name') { sort_by { |item| item[sort.to_sym] } }
+ filters = Fixtures::FILTERS + [sort]
+ reducer = Rack::Reducer.new(Fixtures::DB[:artists], *filters)
+
+ lambda do |env|
+ req = Rack::Request.new(env)
+ res = reducer.apply(req.params).to_json
+ [200, { 'Content-Type' => 'application/json' }, [res]]
+ end
end
- end
- it 'can override default params' do
- get '/artists?sort=genre' do |response|
- genre = response.json[0]['genre']
- expect(genre).to eq('alt-soul')
+ it 'applies default filters' do
+ get '/artists' do |response|
+ name = response.json[0]['name']
+ expect(name).to eq('Björk')
+ end
end
+
+ it 'overrides default filters with values from params' do
+ get '/artists?sort=genre' do |response|
+ genre = response.json[0]['genre']
+ expect(genre).to eq('alt-soul')
+ end
+ end
end
describe 'ad-hoc style via ::call' do
let(:params) { { 'genre' => 'electronic', 'name' => 'blake' } }
it 'works just like the primary style, but slower' do
@@ -89,25 +103,11 @@
expect(result.count).to eq(1)
expect(result[0][:name]).to eq('James Blake')
end
end
- describe 'mixin-style' do
- before { @warnings = [] }
-
- let(:model) do
- dataset = Fixtures::DB[:artists].dup
- allow(dataset).to(receive(:warn)) { |msg| @warnings << msg }
- dataset.extend Rack::Reducer
- dataset.reduces dataset, filters: Fixtures::FILTERS
- dataset
- end
-
- it 'is still supported, but with a deprecation warning' do
- params = { 'genre' => 'electronic', 'name' => 'blake' }
- expect(model.reduce(params).count).to eq(1)
- expect(@warnings.first).to include('mixin-style is deprecated')
- end
+ it 'aliases ::create and ::new' do
+ expect(Rack::Reducer.create({}, -> { 'hi' })).to be_a(Rack::Reducer)
end
it 'accepts nested params' do
get('/artists?range[min]=1&range[max]=100') do |response|
expect(response.status).to eq(200)