Skip to content

Commit

Permalink
Update from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
dgdavid committed Mar 11, 2020
1 parent 127d60b commit 1586993
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
15 changes: 6 additions & 9 deletions library/cwm/src/lib/cwm/multi_status_selector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,10 @@ def contents

# @macro seeAbstractWidget
def handle(event)
if event["ID"].to_s.match?(Item.event_id)
if event["ID"].to_s.include?(Item.event_id)
id, fired_by = event["ID"].split(Item.event_id)

item = find_item(id)

return unless item

send("#{fired_by}_event_handler", item)
end

Expand Down Expand Up @@ -166,10 +163,10 @@ def label_event_handler(item)

# Returns the item with given id
#
# @param id [String] the id of the searched item
# @return [Item, nil] the item matching with given id
def find_item(id)
items.find { |i| i.id.to_s == id }
# @param needle [#to_s] any object that responds to `#to_s`
# @return [Item, nil] the item which id matches with given object#to_s
def find_item(needle)
items.find { |i| i.id.to_s == needle.to_s }
end

# Updates the content based on items list
Expand Down Expand Up @@ -390,7 +387,7 @@ def auto_select!
# One for the check box input and another for the label.
#
# @return [String] the item richtext representation
def to_s
def to_richtext
"#{checkbox_input} #{checkbox_label}"
end

Expand Down
40 changes: 20 additions & 20 deletions library/cwm/test/multi_status_selector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,38 +229,38 @@ def initialize(id, status, enabled)
end
end

describe "#to_s" do
describe "#to_richtext" do
it "returns a string" do
expect(subject.to_s).to be_a(String)
expect(subject.to_richtext).to be_a(String)
end

context "when the item is enabled" do
it "includes a link for the input" do
expect(subject.to_s).to match(regexp_input_link)
expect(subject.to_richtext).to match(regexp_input_link)
end

it "includes a link for the label" do
expect(subject.to_s).to match(regexp_label_link)
expect(subject.to_richtext).to match(regexp_label_link)
end
end

context "when the item is not enabled" do
let(:enabled) { false }

it "uses a grey color" do
expect(subject.to_s).to match(/.*color: grey.*/)
expect(subject.to_richtext).to match(/.*color: grey.*/)
end

it "includes the item label" do
expect(subject.to_s).to include(subject.label)
expect(subject.to_richtext).to include(subject.label)
end

it "does not include a link for the input" do
expect(subject.to_s).to_not match(regexp_input_link)
expect(subject.to_richtext).to_not match(regexp_input_link)
end

it "does not include a link for the label" do
expect(subject.to_s).to_not match(regexp_label_link)
expect(subject.to_richtext).to_not match(regexp_label_link)
end
end

Expand All @@ -271,31 +271,31 @@ def initialize(id, status, enabled)
let(:status) { :selected }

it "displays `[x]` as icon" do
expect(subject.to_s).to include("[x]")
expect(subject.to_richtext).to include("[x]")
end
end

context "and the item is auto selected" do
let(:status) { :auto_selected }

it "displays `[a]` as icon" do
expect(subject.to_s).to include("[a]")
expect(subject.to_richtext).to include("[a]")
end
end

context "and the item is not selected" do
let(:status) { :unselected }

it "displays `[ ]` as icon" do
expect(subject.to_s).to include("[ ]")
expect(subject.to_richtext).to include("[ ]")
end
end

context "and the item has an unknown status" do
let(:status) { :unknown }

it "displays `[ ]` as icon" do
expect(subject.to_s).to include("[ ]")
expect(subject.to_richtext).to include("[ ]")
end
end
end
Expand All @@ -306,32 +306,32 @@ def initialize(id, status, enabled)
context "and the item is selected" do
let(:status) { :selected }

it "displays the proper icon" do
expect(subject.to_s).to include("checkbox-on.svg")
it "displays the selected icon" do
expect(subject.to_richtext).to include("checkbox-on.svg")
end
end

context "and the item is auto selected" do
let(:status) { :auto_selected }

it "displays the proper icon" do
expect(subject.to_s).to include("auto-selected.svg")
it "displays the auto-selected icon" do
expect(subject.to_richtext).to include("auto-selected.svg")
end
end

context "and the item is not selected" do
let(:status) { :unselected }

it "displays the proper ion" do
expect(subject.to_s).to include("checkbox-off.svg")
it "displays the unselected icon" do
expect(subject.to_richtext).to include("checkbox-off.svg")
end
end

context "and the item has an unknown status" do
let(:status) { :unknown }

it "displays the proper ion" do
expect(subject.to_s).to include("checkbox-off.svg")
it "displays the unselected icon" do
expect(subject.to_richtext).to include("checkbox-off.svg")
end
end
end
Expand Down

0 comments on commit 1586993

Please sign in to comment.