discourse/spec/serializers/directory_item_serializer_spec.rb
David Taylor c9dab6fd08
DEV: Automatically require 'rails_helper' in all specs (#16077)
It's very easy to forget to add `require 'rails_helper'` at the top of every core/plugin spec file, and omissions can cause some very confusing/sporadic errors.

By setting this flag in `.rspec`, we can remove the need for `require 'rails_helper'` entirely.
2022-03-01 17:50:50 +00:00

35 lines
1.2 KiB
Ruby

# frozen_string_literal: true
RSpec.describe DirectoryItemSerializer do
fab!(:user) { Fabricate(:user) }
before do
DirectoryItem.refresh!
end
let :serializer do
directory_item = DirectoryItem.find_by(user: user, period_type: DirectoryItem.period_types[:all])
DirectoryItemSerializer.new(directory_item, { attributes: DirectoryColumn.active_column_names })
end
it "Serializes attributes for enabled directory_columns" do
DirectoryColumn.update_all(enabled: true)
payload = serializer.as_json
expect(payload[:directory_item].keys).to include(*DirectoryColumn.pluck(:name).map(&:to_sym))
end
it "Doesn't serialize attributes for disabled directory columns" do
DirectoryColumn.update_all(enabled: false)
directory_column = DirectoryColumn.first
directory_column.update(enabled: true)
payload = serializer.as_json
expect(payload[:directory_item].keys.count).to eq(4)
expect(payload[:directory_item]).to have_key(directory_column.name.to_sym)
expect(payload[:directory_item]).to have_key(:id)
expect(payload[:directory_item]).to have_key(:user)
expect(payload[:directory_item]).to have_key(:time_read)
end
end