Module: PlanMyStuff::TestHelpers

Defined in:
lib/plan_my_stuff/test_helpers.rb

Overview

Test support for consuming apps. Provides:

  • ‘PlanMyStuff.test_mode!` to stub all API calls

  • Factory-style builders: ‘build_issue`, `build_comment`, `build_project`

  • RSpec matchers: ‘expect_pms_issue_created`, `expect_pms_comment_created`, `expect_pms_item_moved`

Usage:

require 'plan_my_stuff/test_helpers'

RSpec.configure do |config|
  config.include PlanMyStuff::TestHelpers
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_comment(body: 'Test comment', visibility: :public, id: 1, issue: nil, metadata: {}) ⇒ PlanMyStuff::Comment

Builds a fake persisted Comment without hitting the API.

Parameters:

  • body (String) (defaults to: 'Test comment')
  • visibility (Symbol, String) (defaults to: :public)
  • id (Integer) (defaults to: 1)
  • issue (PlanMyStuff::Issue, nil) (defaults to: nil)

    parent issue (auto-built if nil)

  • metadata (Hash) (defaults to: {})

    raw metadata fields to merge

Returns:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/plan_my_stuff/test_helpers.rb', line 84

def build_comment(body: 'Test comment', visibility: :public, id: 1, issue: nil, metadata: {})
  issue ||= build_issue

  comment = PlanMyStuff::Comment.new(
    body: body,
    id: id,
    issue: issue,
  )
  comment.visibility = visibility.to_sym

   = PlanMyStuff::CommentMetadata.from_hash({
    schema_version: PlanMyStuff::BaseMetadata::SCHEMA_VERSION,
    gem_version: PlanMyStuff::VERSION::STRING,
    visibility: visibility.to_s,
    custom_fields: {},
  }.merge())

  comment.instance_variable_set(:@metadata, )
  comment.instance_variable_set(:@persisted, true)
  comment
end

.build_issue(title: 'Test Issue', body: 'Test body', state: :open, number: 1, repo: 'TestOrg/TestRepo', labels: [], metadata: {}) ⇒ PlanMyStuff::Issue

Builds a fake persisted Issue without hitting the API.

Parameters:

  • title (String) (defaults to: 'Test Issue')
  • body (String) (defaults to: 'Test body')
  • state (Symbol, String) (defaults to: :open)
  • number (Integer) (defaults to: 1)
  • repo (String) (defaults to: 'TestOrg/TestRepo')
  • labels (Array<String>) (defaults to: [])
  • metadata (Hash) (defaults to: {})

    raw metadata fields to merge

Returns:



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
# File 'lib/plan_my_stuff/test_helpers.rb', line 36

def build_issue(
  title: 'Test Issue',
  body: 'Test body',
  state: :open,
  number: 1,
  repo: 'TestOrg/TestRepo',
  labels: [],
  metadata: {}
)
  issue = PlanMyStuff::Issue.new(
    title: title,
    body: body,
    state: state.to_s,
    number: number,
    repo: repo,
    labels: labels,
  )

   = PlanMyStuff::IssueMetadata.from_hash({
    schema_version: PlanMyStuff::BaseMetadata::SCHEMA_VERSION,
    gem_version: PlanMyStuff::VERSION::STRING,
    visibility: 'public',
    custom_fields: {},
  }.merge())

  issue.instance_variable_set(:@metadata, )
  issue.instance_variable_set(:@persisted, true)

  body_comment = build_comment(
    body: body,
    issue: issue,
    metadata: { issue_body: true },
  )
  issue.instance_variable_set(:@comments, [body_comment])

  issue
end

.build_project(title: 'Test Project', number: 1, statuses: [], items: []) ⇒ PlanMyStuff::Project

Builds a fake persisted Project without hitting the API.

Parameters:

  • title (String) (defaults to: 'Test Project')
  • number (Integer) (defaults to: 1)
  • statuses (Array<String>) (defaults to: [])

    status names (auto-assigned IDs)

  • items (Array<Hash>) (defaults to: [])

    item data hashes

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/plan_my_stuff/test_helpers.rb', line 115

def build_project(title: 'Test Project', number: 1, statuses: [], items: [])
  status_options = statuses.each_with_index.map do |name, i|
    { id: "status_option_#{i}", name: name }
  end

  project = PlanMyStuff::Project.new(
    id: "PVT_fake_#{number}",
    number: number,
    title: title,
    statuses: status_options,
    fields: [{ id: 'field_status', name: 'Status', options: status_options }],
  )
  project.instance_variable_set(:@persisted, true)

  project.items = items.map do |item_hash|
    PlanMyStuff::ProjectItem.build(
      {
        id: item_hash[:id] || "PVTI_fake_#{rand(10_000)}",
        title: item_hash[:title] || 'Untitled',
        number: item_hash[:number],
        url: item_hash[:url],
        state: item_hash[:state],
        status: item_hash[:status],
        field_values: item_hash[:field_values] || {},
      },
      project: project,
    )
  end

  project
end

Instance Method Details

#expect_pms_comment_created(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. issue_number:, visibility:)



182
183
184
# File 'lib/plan_my_stuff/test_helpers.rb', line 182

def expect_pms_comment_created(**filters)
  expect_pms_action(:comment_created, 'comment to be created', **filters)
end

#expect_pms_comment_updated(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. id:, body:)



192
193
194
# File 'lib/plan_my_stuff/test_helpers.rb', line 192

def expect_pms_comment_updated(**filters)
  expect_pms_action(:comment_updated, 'comment to be updated', **filters)
end

#expect_pms_comments_listed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. issue_number:, pms_only:)



187
188
189
# File 'lib/plan_my_stuff/test_helpers.rb', line 187

def expect_pms_comments_listed(**filters)
  expect_pms_action(:comments_listed, 'comments to be listed', **filters)
end

#expect_pms_issue_created(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. repo:, title:)



152
153
154
# File 'lib/plan_my_stuff/test_helpers.rb', line 152

def expect_pms_issue_created(**filters)
  expect_pms_action(:issue_created, 'issue to be created', **filters)
end

#expect_pms_issue_found(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, repo:)



157
158
159
# File 'lib/plan_my_stuff/test_helpers.rb', line 157

def expect_pms_issue_found(**filters)
  expect_pms_action(:issue_found, 'issue to be found', **filters)
end

#expect_pms_issue_updated(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, state:)



167
168
169
# File 'lib/plan_my_stuff/test_helpers.rb', line 167

def expect_pms_issue_updated(**filters)
  expect_pms_action(:issue_updated, 'issue to be updated', **filters)
end

#expect_pms_issues_listed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters



162
163
164
# File 'lib/plan_my_stuff/test_helpers.rb', line 162

def expect_pms_issues_listed(**filters)
  expect_pms_action(:issues_listed, 'issues to be listed', **filters)
end

#expect_pms_item_assigned(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. project:, assignee:)



217
218
219
# File 'lib/plan_my_stuff/test_helpers.rb', line 217

def expect_pms_item_assigned(**filters)
  expect_pms_action(:item_assigned, 'item to be assigned', **filters)
end

#expect_pms_item_created(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. project:, title:)



212
213
214
# File 'lib/plan_my_stuff/test_helpers.rb', line 212

def expect_pms_item_created(**filters)
  expect_pms_action(:item_created, 'item to be created', **filters)
end

#expect_pms_item_moved(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. project:, status:)



207
208
209
# File 'lib/plan_my_stuff/test_helpers.rb', line 207

def expect_pms_item_moved(**filters)
  expect_pms_action(:item_moved, 'item to be moved', **filters)
end

#expect_pms_project_found(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:)



202
203
204
# File 'lib/plan_my_stuff/test_helpers.rb', line 202

def expect_pms_project_found(**filters)
  expect_pms_action(:project_found, 'project to be found', **filters)
end

#expect_pms_projects_listed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters



197
198
199
# File 'lib/plan_my_stuff/test_helpers.rb', line 197

def expect_pms_projects_listed(**filters)
  expect_pms_action(:projects_listed, 'projects to be listed', **filters)
end

#expect_pms_viewers_added(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, user_ids:)



172
173
174
# File 'lib/plan_my_stuff/test_helpers.rb', line 172

def expect_pms_viewers_added(**filters)
  expect_pms_action(:viewers_added, 'viewers to be added', **filters)
end

#expect_pms_viewers_removed(**filters) ⇒ Object

Parameters:

  • filters (Hash)

    attribute filters (e.g. number:, user_ids:)



177
178
179
# File 'lib/plan_my_stuff/test_helpers.rb', line 177

def expect_pms_viewers_removed(**filters)
  expect_pms_action(:viewers_removed, 'viewers to be removed', **filters)
end