Skip to content

Commit

Permalink
test coverage for sorting fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alameenkhader committed Aug 18, 2020
1 parent 5f5c3c9 commit ad291ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
26 changes: 15 additions & 11 deletions lib/apidoco/folder_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,10 @@ def initialize(directory, parents: [])
end

def as_json
childs = children.select(&:published?).sort do |a, b|
if a.sort_order.class == b.sort_order.class
a.sort_order <=> b.sort_order
elsif a.sort_order.is_a?(String)
-1
else
1
end
end

{
is_folder: true,
name: basename,
children: childs.map(&:as_json)
children: children.select(&:published?).yield_self(&method(:sorter)).map(&:as_json)
}
end

Expand All @@ -48,5 +38,19 @@ def published?
def sort_order
basename
end

private

def sorter(files)
files.sort do |a, b|
if a.sort_order.class == b.sort_order.class
a.sort_order <=> b.sort_order
elsif a.sort_order.is_a?(String)
-1
else
1
end
end
end
end
end
33 changes: 29 additions & 4 deletions spec/apidoco/folder_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
f.rewind
end
end
let(:directory) do
double(basename: 'users', children: [file])
end
let(:directory) { double(basename: 'users', children: [file]) }
let(:folder_parser) { described_class.new(directory, parents: []) }

after do
Expand All @@ -32,7 +30,34 @@
end

describe 'children' do
it { expect(as_json[:children].first).to include(id: 'users-createuser', 'name' => 'Create User') }
let(:sub_directory1) { double(basename: 'roles', children: [], directory?: true) }
let(:sub_directory2) { double(basename: 'departments', children: [], directory?: true) }
let(:file1) { file }
let(:file2) do
Tempfile.new.tap do |f|
f.write({ name: 'Delete User' }.to_json)
f.rewind
end
end

before { allow(file2).to receive(:directory?).and_return(false) }

after do
file2.close
file2.unlink
end

let(:directory) do
double(basename: 'users', children: [file1, file2, sub_directory1, sub_directory2])
end

it 'sorts the files and folders based on the sort order' do
expect(as_json).to include(name: 'Users')
expect(as_json[:children][0][:name]).to eq('Departments')
expect(as_json[:children][1][:name]).to eq('Roles')
expect(as_json[:children][2]['name']).to eq('Create User')
expect(as_json[:children][3]['name']).to eq('Delete User')
end
end
end

Expand Down

0 comments on commit ad291ce

Please sign in to comment.