|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'rails_helper' |
| 4 | + |
| 5 | +RSpec.describe 'mutation UpdateComponent() { ... }' do |
| 6 | + subject(:result) { execute_query(query: mutation, variables:) } |
| 7 | + |
| 8 | + let(:mutation) { 'mutation UpdateComponent($component: UpdateComponentInput!) { updateComponent(input: $component) { component { id } } }' } |
| 9 | + let(:component_id) { 'dummy-id' } |
| 10 | + let(:variables) do |
| 11 | + { |
| 12 | + component: { |
| 13 | + id: component_id, |
| 14 | + name: 'main2', |
| 15 | + extension: 'py', |
| 16 | + content: '', |
| 17 | + default: false |
| 18 | + } |
| 19 | + } |
| 20 | + end |
| 21 | + |
| 22 | + it { expect(mutation).to be_a_valid_graphql_query } |
| 23 | + |
| 24 | + context 'with an existing component' do |
| 25 | + let(:component) { create(:component, name: 'bob', extension: 'html', content: 'new', default: true) } |
| 26 | + let(:component_id) { component.to_gid_param } |
| 27 | + |
| 28 | + before do |
| 29 | + # Instantiate component |
| 30 | + component |
| 31 | + end |
| 32 | + |
| 33 | + context 'when unauthenticated' do |
| 34 | + it 'does not update a component' do |
| 35 | + expect { result }.not_to change { component.reload.name } |
| 36 | + end |
| 37 | + |
| 38 | + it 'returns an error' do |
| 39 | + expect(result.dig('errors', 0, 'message')).not_to be_blank |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + context 'when the graphql context is unset' do |
| 44 | + let(:graphql_context) { nil } |
| 45 | + |
| 46 | + it 'does not update a component' do |
| 47 | + expect { result }.not_to change { component.reload.name } |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + context 'when authenticated' do |
| 52 | + let(:current_user_id) { component.project.user_id } |
| 53 | + |
| 54 | + it 'updates the component name' do |
| 55 | + expect { result }.to change { component.reload.name }.from(component.name).to(variables.dig(:component, :name)) |
| 56 | + end |
| 57 | + |
| 58 | + it 'updates the component content' do |
| 59 | + expect { result }.to change { component.reload.content }.from(component.content).to(variables.dig(:component, :content)) |
| 60 | + end |
| 61 | + |
| 62 | + it 'updates the component extension' do |
| 63 | + expect { result }.to change { component.reload.extension }.from(component.extension).to(variables.dig(:component, :extension)) |
| 64 | + end |
| 65 | + |
| 66 | + it 'updates the component default' do |
| 67 | + expect { result }.to change { component.reload.default }.from(component.default).to(variables.dig(:component, :default)) |
| 68 | + end |
| 69 | + |
| 70 | + context 'when the component cannot be found' do |
| 71 | + let(:component_id) { 'dummy' } |
| 72 | + |
| 73 | + it 'returns an error' do |
| 74 | + expect(result.dig('errors', 0, 'message')).to match(/not found/) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + context 'with another users component' do |
| 79 | + let(:current_user_id) { SecureRandom.uuid } |
| 80 | + |
| 81 | + it 'returns an error' do |
| 82 | + expect(result.dig('errors', 0, 'message')).to match(/not permitted/) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context 'when component update fails' do |
| 87 | + before do |
| 88 | + errors = instance_double(ActiveModel::Errors, full_messages: ['An error message']) |
| 89 | + allow(component).to receive(:save).and_return(false) |
| 90 | + allow(component).to receive(:errors).and_return(errors) |
| 91 | + allow(GlobalID).to receive(:find).and_return(component) |
| 92 | + end |
| 93 | + |
| 94 | + it 'returns an error' do |
| 95 | + expect(result.dig('errors', 0, 'message')).to match(/An error message/) |
| 96 | + end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | +end |
0 commit comments