2017-09-07 13:08:56 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe GroupChildSerializer do
|
|
|
|
let(:request) { double('request') }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
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
|
|
|
|
|
2017-09-08 13:22:33 -04:00
|
|
|
context 'with a hierarchy', :nested_groups 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)
|
|
|
|
|
|
|
|
json = serializer.represent(subsub_group)
|
|
|
|
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)
|
|
|
|
subsub_group2 = create(:group, parent: subgroup2)
|
2017-09-07 13:08:56 -04:00
|
|
|
|
2017-09-08 13:22:33 -04:00
|
|
|
json = serializer.represent([subsub_group1, subsub_group2])
|
|
|
|
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)
|
|
|
|
|
|
|
|
json = serializer.represent([subgroup])
|
|
|
|
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
|
|
|
|
|
|
|
|
context 'with a hierarchy', :nested_groups do
|
|
|
|
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)
|
|
|
|
|
|
|
|
json = serializer.represent([project1, project2])
|
|
|
|
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-09-07 13:08:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|