aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/trix_attachment_test.rb
blob: 9cb338b8c2d7aa573f17bdb3a138bd66650e7987 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# frozen_string_literal: true

require 'test_helper'

class ActionText::TrixAttachmentTest < ActiveSupport::TestCase
  test "from_attributes" do
    attributes = {
      "data-trix-attachment" => {
        "sgid" => "123",
        "contentType" => "text/plain",
        "href" => "http://example.com/",
        "filename" => "example.txt",
        "filesize" => 12345,
        "previewable" => true
      },
      "data-trix-attributes" => {
        "caption" => "hello"
      }
    }

    attachment = attachment(
      sgid: "123",
      content_type: "text/plain",
      href: "http://example.com/",
      filename: "example.txt",
      filesize: "12345",
      previewable: "true",
      caption: "hello"
    )

    assert_attachment_json_attributes(attachment, attributes)
  end

  test "previewable is typecast" do
    assert_attachment_attribute(attachment(previewable: ""), "previewable", false)
    assert_attachment_attribute(attachment(previewable: false), "previewable", false)
    assert_attachment_attribute(attachment(previewable: "false"), "previewable", false)
    assert_attachment_attribute(attachment(previewable: "garbage"), "previewable", false)
    assert_attachment_attribute(attachment(previewable: true), "previewable", true)
    assert_attachment_attribute(attachment(previewable: "true"), "previewable", true)
  end

  test "filesize is typecast when integer-like" do
    assert_attachment_attribute(attachment(filesize: 123), "filesize", 123)
    assert_attachment_attribute(attachment(filesize: "123"), "filesize", 123)
    assert_attachment_attribute(attachment(filesize: "3.5 MB"), "filesize", "3.5 MB")
    assert_attachment_attribute(attachment(filesize: nil), "filesize", nil)
    assert_attachment_attribute(attachment(filesize: ""), "filesize", "")
  end

  test "#attributes strips unmappable attributes" do
    attributes = {
      "sgid" => "123",
      "caption" => "hello"
    }

    attachment = attachment(sgid: "123", caption: "hello", nonexistent: "garbage")
    assert_attachment_attributes(attachment, attributes)
  end

  def assert_attachment_attribute(attachment, name, value)
    if value.nil?
      assert_nil(attachment.attributes[name])
    else
      assert_equal(value, attachment.attributes[name])
    end
  end

  def assert_attachment_attributes(attachment, attributes)
    assert_equal(attributes, attachment.attributes)
  end

  def assert_attachment_json_attributes(attachment, attributes)
    attributes.each do |name, expected|
      actual = JSON.parse(attachment.node[name])
      assert_equal(expected, actual)
    end
  end

  def attachment(**attributes)
    ActionText::TrixAttachment.from_attributes(attributes)
  end
end