require 'inline_svg/transform_pipeline' describe InlineSvg::TransformPipeline::Transformations::AriaAttributes do it "adds a role attribute to the SVG document" do document = Nokogiri::XML::Document.parse('Some document') transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value({}) expect(transformation.transform(document).to_html).to eq( "Some document\n" ) end context "aria-labelledby attribute" do it "adds 'title' when a title element is present" do document = Nokogiri::XML::Document.parse('Some titleSome document') transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt") expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("title", "some-salt"). and_return("some-id") expect(transformation.transform(document).to_html).to eq( "Some titleSome document\n" ) end it "adds 'desc' when a description element is present" do document = Nokogiri::XML::Document.parse('Some descriptionSome document') transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt") expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("desc", "some-salt"). and_return("some-id") expect(transformation.transform(document).to_html).to eq( "Some descriptionSome document\n" ) end it "adds both 'desc' and 'title' when title and description elements are present" do document = Nokogiri::XML::Document.parse('Some titleSome descriptionSome document') transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt") expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("title", "some-salt"). and_return("some-id") expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("desc", "some-salt"). and_return("some-other-id") expect(transformation.transform(document).to_html).to eq( "Some title\nSome descriptionSome document\n" ) end it "uses existing IDs when they exist" do document = Nokogiri::XML::Document.parse('Some titleSome descriptionSome document') transformation = InlineSvg::TransformPipeline::Transformations::AriaAttributes.create_with_value("some-salt") expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("my-title", "some-salt"). and_return("some-id") expect(InlineSvg::RandomIdGenerator).to receive(:generate).with("my-desc", "some-salt"). and_return("some-other-id") expect(transformation.transform(document).to_html).to eq( "Some title\nSome descriptionSome document\n" ) end end end