From ad291ceadcb5bc1f2cf481bcf0277a915ee9cebf Mon Sep 17 00:00:00 2001 From: alameenkhader Date: Tue, 18 Aug 2020 08:44:18 -0700 Subject: [PATCH] test coverage for sorting fix --- lib/apidoco/folder_parser.rb | 26 +++++++++++++---------- spec/apidoco/folder_parser_spec.rb | 33 ++++++++++++++++++++++++++---- 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/lib/apidoco/folder_parser.rb b/lib/apidoco/folder_parser.rb index 4ac94c1..d93eb6e 100644 --- a/lib/apidoco/folder_parser.rb +++ b/lib/apidoco/folder_parser.rb @@ -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 @@ -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 diff --git a/spec/apidoco/folder_parser_spec.rb b/spec/apidoco/folder_parser_spec.rb index a007d30..99adf37 100644 --- a/spec/apidoco/folder_parser_spec.rb +++ b/spec/apidoco/folder_parser_spec.rb @@ -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 @@ -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