2019-09-30 05:06:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-07 13:08:56 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 02:09:01 -04:00
|
|
|
RSpec.describe GroupChildSerializer do
|
2017-09-07 13:08:56 -04:00
|
|
|
let(:request) { double('request') }
|
|
|
|
let(:user) { create(:user) }
|
2019-12-17 13:07:48 -05:00
|
|
|
|
2017-09-07 13:08:56 -04:00
|
|
|
subject(:serializer) { described_class.new(current_user: user) }
|
|
|
|
|
|
|
|
describe '#represent' do
|
|
|
|
context 'for groups' do
|
|
|
|
it 'can render a single group' do
|
|
|
|
expect(serializer.represent(build(:group))).to be_kind_of(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can render a collection of groups' do
|
|
|
|
expect(serializer.represent(build_list(:group, 2))).to be_kind_of(Array)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
context 'with a hierarchy' do
|
2017-09-07 13:08:56 -04:00
|
|
|
let(:parent) { create(:group) }
|
|
|
|
|
|
|
|
subject(:serializer) do
|
|
|
|
described_class.new(current_user: user).expand_hierarchy(parent)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'expands the subgroups' do
|
|
|
|
subgroup = create(:group, parent: parent)
|
|
|
|
subsub_group = create(:group, parent: subgroup)
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
json = serializer.represent([subgroup, subsub_group]).first
|
2017-09-07 13:08:56 -04:00
|
|
|
subsub_group_json = json[:children].first
|
|
|
|
|
|
|
|
expect(json[:id]).to eq(subgroup.id)
|
|
|
|
expect(subsub_group_json).not_to be_nil
|
|
|
|
expect(subsub_group_json[:id]).to eq(subsub_group.id)
|
|
|
|
end
|
|
|
|
|
2017-09-08 13:22:33 -04:00
|
|
|
it 'can render a nested tree' do
|
|
|
|
subgroup1 = create(:group, parent: parent)
|
|
|
|
subsub_group1 = create(:group, parent: subgroup1)
|
|
|
|
subgroup2 = create(:group, parent: parent)
|
2017-09-07 13:08:56 -04:00
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
json = serializer.represent([subgroup1, subsub_group1, subgroup1, subgroup2])
|
2017-09-08 13:22:33 -04:00
|
|
|
subgroup1_json = json.first
|
|
|
|
subsub_group1_json = subgroup1_json[:children].first
|
|
|
|
|
|
|
|
expect(json.size).to eq(2)
|
|
|
|
expect(subgroup1_json[:id]).to eq(subgroup1.id)
|
|
|
|
expect(subsub_group1_json[:id]).to eq(subsub_group1.id)
|
|
|
|
end
|
2017-09-12 08:58:44 -04:00
|
|
|
|
|
|
|
context 'without a specified parent' do
|
|
|
|
subject(:serializer) do
|
|
|
|
described_class.new(current_user: user).expand_hierarchy
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can render a tree' do
|
|
|
|
subgroup = create(:group, parent: parent)
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
json = serializer.represent([parent, subgroup])
|
2017-09-12 08:58:44 -04:00
|
|
|
parent_json = json.first
|
|
|
|
|
|
|
|
expect(parent_json[:id]).to eq(parent.id)
|
|
|
|
expect(parent_json[:children].first[:id]).to eq(subgroup.id)
|
|
|
|
end
|
|
|
|
end
|
2017-09-08 13:22:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'for projects' do
|
|
|
|
it 'can render a single project' do
|
|
|
|
expect(serializer.represent(build(:project))).to be_kind_of(Hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can render a collection of projects' do
|
|
|
|
expect(serializer.represent(build_list(:project, 2))).to be_kind_of(Array)
|
|
|
|
end
|
|
|
|
|
2019-07-24 05:20:54 -04:00
|
|
|
context 'with a hierarchy' do
|
2017-09-08 13:22:33 -04:00
|
|
|
let(:parent) { create(:group) }
|
|
|
|
|
|
|
|
subject(:serializer) do
|
|
|
|
described_class.new(current_user: user).expand_hierarchy(parent)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'can render a nested tree' do
|
|
|
|
subgroup1 = create(:group, parent: parent)
|
|
|
|
project1 = create(:project, namespace: subgroup1)
|
|
|
|
subgroup2 = create(:group, parent: parent)
|
|
|
|
project2 = create(:project, namespace: subgroup2)
|
|
|
|
|
2017-10-04 10:57:33 -04:00
|
|
|
json = serializer.represent([project1, project2, subgroup1, subgroup2])
|
2017-09-08 13:22:33 -04:00
|
|
|
project1_json, project2_json = json.map { |group_json| group_json[:children].first }
|
|
|
|
|
|
|
|
expect(json.size).to eq(2)
|
|
|
|
expect(project1_json[:id]).to eq(project1.id)
|
|
|
|
expect(project2_json[:id]).to eq(project2.id)
|
|
|
|
end
|
2017-10-13 09:13:52 -04:00
|
|
|
|
|
|
|
it 'returns an array when an array of a single instance was given' do
|
|
|
|
project = create(:project, namespace: parent)
|
|
|
|
|
|
|
|
json = serializer.represent([project])
|
|
|
|
|
|
|
|
expect(json).to be_kind_of(Array)
|
|
|
|
expect(json.size).to eq(1)
|
|
|
|
end
|
2017-09-07 13:08:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|