chore: add specs

This commit is contained in:
Muhsin Keloth
2024-05-08 10:51:10 +05:30
parent e098e463ad
commit 17b61ad2b4
2 changed files with 140 additions and 16 deletions
@@ -2,31 +2,68 @@ require 'rails_helper'
describe Integrations::Linear::ProcessorService do
let(:account) { create(:account) }
let(:processor) { described_class.new(account: account) }
let(:api_key) { 'valid_api_key' }
let(:url) { 'https://api.linear.app/graphql' }
let(:headers) { { 'Content-Type' => 'application/json', 'Authorization' => api_key } }
let(:schema) { JSON.parse(File.read('spec/fixtures/linear-schema.json')) }
let(:linear_client) { Graphlient::Client.new(url, schema: Graphlient::Schema.new({ 'data' => schema }, nil), headers: headers) }
let(:linear_instance) { Linear.new(api_key) }
let(:linear_client) { instance_double(Linear) }
let(:service) { described_class.new(account: account) }
before do
create(:integrations_hook, :linear, account: account)
allow(Linear).to receive(:new).and_return(linear_client)
end
describe '#teams' do
context 'when the API response is success' do
before do
linear_instance.instance_variable_set(:@client, linear_client)
processor.instance_variable_set(:@linear_client, linear_instance)
stub_request(:post, url)
.to_return(status: 200, body: { data: { teams: { nodes: [{ id: 'team1', name: 'Team One' }] } } }.to_json)
context 'when Linear client returns valid data' do
let(:teams_response) do
{ 'teams' => { 'nodes' => [{ 'id' => 'team1', 'name' => 'Team One' }] } }
end
it 'returns the list of teams' do
result = processor.teams
expect(result).to eq([{ 'id' => 'team1', 'name' => 'Team One' }])
it 'returns parsed team data' do
allow(linear_client).to receive(:teams).and_return(teams_response)
result = service.teams
expect(result).to contain_exactly({ 'id' => 'team1', 'name' => 'Team One' })
end
end
context 'when Linear client returns an error' do
let(:error_response) { { error: 'Some error message' } }
it 'returns the error' do
allow(linear_client).to receive(:teams).and_return(error_response)
result = service.teams
expect(result).to eq(error_response)
end
end
end
describe '#team_entites' do
let(:team_id) { 'team1' }
let(:entities_response) do
{
'users' => { 'nodes' => [{ 'id' => 'user1', 'name' => 'User One' }] },
'projects' => { 'nodes' => [{ 'id' => 'project1', 'name' => 'Project One' }] },
'workflowStates' => { 'nodes' => [] },
'issueLabels' => { 'nodes' => [{ 'id' => 'bug', 'name' => 'Bug' }] }
}
end
context 'when Linear client returns valid data' do
it 'returns parsed entity data' do
allow(linear_client).to receive(:team_entites).with(team_id).and_return(entities_response)
result = service.team_entites(team_id)
expect(result).to have_key(:users)
expect(result).to have_key(:projects)
expect(result).to have_key(:states)
expect(result).to have_key(:labels)
end
end
context 'when Linear client returns an error' do
let(:error_response) { { error: 'Some error message' } }
it 'returns the error' do
allow(linear_client).to receive(:team_entites).with(team_id).and_return(error_response)
result = service.team_entites(team_id)
expect(result).to eq(error_response)
end
end
end