Last active
April 9, 2020 10:49
-
-
Save Kukunin/cc9c3efe0a9e95f7df28f90e57c1008d to your computer and use it in GitHub Desktop.
A test to create a tree of elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe 'hierarchy' do | |
subject(:hierarchy) { create_hierarchy(per_level: 3, levels: 5) } | |
def create_element(type, children) | |
{ type: type, children: children } | |
end | |
# Take a pen and piece of paper | |
# Implement here | |
def create_hierarchy(per_level:, levels:) | |
return [create_element('category', [])] | |
end | |
# End your implementation here | |
def child(node) | |
node[:children].first | |
end | |
let(:parent) { hierarchy.first } | |
let(:child_1) { child(parent) } | |
let(:child_2) { child(child_1) } | |
let(:child_3) { child(child_2) } | |
let(:child_4) { child(child_3) } | |
it { expect(hierarchy).to have_attributes(size: 3) } | |
it { expect(parent).to include(children: have_attributes(size: 3)) } | |
it { expect(parent).to include(type: 'category') } | |
it { expect(child_1).to include(children: have_attributes(size: 3)) } | |
it { expect(child_1).to include(type: 'category') } | |
it { expect(child_2).to include(children: have_attributes(size: 3)) } | |
it { expect(child_2).to include(type: 'topic') } | |
it { expect(child_3).to include(children: have_attributes(size: 3)) } | |
it { expect(child_3).to include(type: 'topic') } | |
it { expect(child_4).to include(children: have_attributes(size: 0)) } | |
it { expect(child_4).to include(type: 'topic') } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The goal of this task is to implement the
create_hierarchy
function, which returns an array that hasper_level
elements. Each of that contains own list of elements as children. The amount of levels is defined bylevels
argument.ADDITIONAL REQUIREMENT: elements in the first two levels should have type
category
while elements since the third level should have typetopic
.You can copy this spec inside any Rails project with RSpec and run it as a regular test. There are no other dependencies other than RSpec.
Example of the output for
per_level:3, levels: 5
: