|
3 | 3 | require 'rails_helper' |
4 | 4 |
|
5 | 5 | RSpec.describe Project::Operation::CreateRemix, type: :unit do |
6 | | - subject(:create_remix) { described_class.call(params, user_id) } |
| 6 | + subject(:create_remix) { described_class.call(params: remix_params, user_id: user_id, original_project: original_project) } |
7 | 7 |
|
8 | 8 | let(:user_id) { 'e0675b6c-dc48-4cd6-8c04-0f7ac05af51a' } |
9 | 9 | let!(:original_project) { create(:project, :with_components) } |
| 10 | + let(:remix_params) do |
| 11 | + component = original_project.components.first |
| 12 | + { |
| 13 | + name: original_project.name, |
| 14 | + identifier: original_project.identifier, |
| 15 | + components: [ |
| 16 | + { |
| 17 | + id: component.id, |
| 18 | + name: component.name, |
| 19 | + extension: component.extension, |
| 20 | + content: 'some updated component content', |
| 21 | + index: component.index |
| 22 | + } |
| 23 | + ] |
| 24 | + } |
| 25 | + end |
10 | 26 |
|
11 | 27 | before do |
12 | 28 | mock_phrase_generation |
13 | 29 | end |
14 | 30 |
|
15 | 31 | describe '.call' do |
16 | | - context 'when all params valid' do |
17 | | - let(:params) { { project_id: original_project.identifier } } |
| 32 | + let(:params) { { project_id: original_project.identifier } } |
18 | 33 |
|
19 | | - it 'returns success' do |
20 | | - result = create_remix |
21 | | - expect(result.success?).to eq(true) |
22 | | - end |
| 34 | + it 'returns success' do |
| 35 | + result = create_remix |
| 36 | + expect(result.success?).to eq(true) |
| 37 | + end |
| 38 | + |
| 39 | + it 'creates new project' do |
| 40 | + expect { create_remix }.to change(Project, :count).by(1) |
| 41 | + end |
| 42 | + |
| 43 | + it 'assigns a new identifer to new project' do |
| 44 | + result = create_remix |
| 45 | + remixed_project = result[:project] |
| 46 | + expect(remixed_project.identifier).not_to eq(original_project.identifier) |
| 47 | + end |
| 48 | + |
| 49 | + it 'assigns user_id to new project' do |
| 50 | + remixed_project = create_remix[:project] |
| 51 | + expect(remixed_project.user_id).to eq(user_id) |
| 52 | + end |
| 53 | + |
| 54 | + it 'duplicates properties on new project' do |
| 55 | + remixed_project = create_remix[:project] |
23 | 56 |
|
24 | | - it 'creates new project' do |
25 | | - expect { create_remix }.to change(Project, :count).by(1) |
| 57 | + remixed_attrs = remixed_project.attributes.symbolize_keys.slice(:name, :project_type) |
| 58 | + original_attrs = original_project.attributes.symbolize_keys.slice(:name, :project_type) |
| 59 | + expect(remixed_attrs).to eq(original_attrs) |
| 60 | + end |
| 61 | + |
| 62 | + it 'creates new components' do |
| 63 | + expect { create_remix }.to change(Component, :count).by(1) |
| 64 | + end |
| 65 | + |
| 66 | + it 'persists changes made to submitted components' do |
| 67 | + remixed_project = create_remix[:project] |
| 68 | + expect(remixed_project.components.first.content).to eq('some updated component content') |
| 69 | + end |
| 70 | + |
| 71 | + context 'when a new component has been added before remixing' do |
| 72 | + let(:new_component_params) { { name: 'added_component', extension: 'py', content: 'some added component content', index: 9999 } } |
| 73 | + |
| 74 | + before do |
| 75 | + remix_params[:components] << new_component_params |
26 | 76 | end |
27 | 77 |
|
28 | | - it 'assigns a new identifer to new project' do |
29 | | - result = create_remix |
30 | | - remixed_project = result[:project] |
31 | | - expect(remixed_project.identifier).not_to eq(original_project.identifier) |
| 78 | + it 'creates all components' do |
| 79 | + expect { create_remix }.to change(Component, :count).by(2) |
32 | 80 | end |
33 | 81 |
|
34 | | - it 'assigns user_id to new project' do |
| 82 | + it 'persists the new component' do |
35 | 83 | remixed_project = create_remix[:project] |
36 | | - expect(remixed_project.user_id).to eq(user_id) |
| 84 | + expect(remixed_project.components.last.attributes.symbolize_keys).to include(new_component_params) |
37 | 85 | end |
| 86 | + end |
38 | 87 |
|
39 | | - it 'duplicates properties on new project' do |
40 | | - remixed_project = create_remix[:project] |
| 88 | + context 'when user_id is not present' do |
| 89 | + let(:user_id) { nil } |
| 90 | + let(:params) { { project_id: original_project.identifier } } |
41 | 91 |
|
42 | | - remixed_attrs = remixed_project.attributes.symbolize_keys.slice(:name, :project_type) |
43 | | - original_attrs = original_project.attributes.symbolize_keys.slice(:name, :project_type) |
44 | | - expect(remixed_attrs).to eq(original_attrs) |
| 92 | + it 'returns failure' do |
| 93 | + result = create_remix |
| 94 | + expect(result.failure?).to eq(true) |
45 | 95 | end |
46 | 96 |
|
47 | | - it 'duplicates project components' do |
48 | | - remixed_props_array = component_array_props(create_remix[:project].components) |
49 | | - original_props_array = component_array_props(original_project.components) |
| 97 | + it 'returns error message' do |
| 98 | + result = create_remix |
| 99 | + expect(result[:error]).to eq(I18n.t('errors.project.remixing.invalid_params')) |
| 100 | + end |
50 | 101 |
|
51 | | - expect(remixed_props_array).to match_array(original_props_array) |
| 102 | + it 'does not create new project' do |
| 103 | + expect { create_remix }.not_to change(Project, :count) |
52 | 104 | end |
53 | 105 | end |
54 | 106 |
|
55 | | - context 'when user_id is not present' do |
56 | | - let(:user_id) { nil } |
57 | | - let(:params) { { project_id: original_project.identifier } } |
| 107 | + context 'when original project is not present' do |
| 108 | + subject(:create_remix) { described_class.call(params: remix_params, user_id: user_id, original_project: nil) } |
58 | 109 |
|
59 | 110 | it 'returns failure' do |
60 | 111 | result = create_remix |
61 | 112 | expect(result.failure?).to eq(true) |
62 | 113 | end |
63 | 114 |
|
| 115 | + it 'returns error message' do |
| 116 | + result = create_remix |
| 117 | + expect(result[:error]).to eq(I18n.t('errors.project.remixing.invalid_params')) |
| 118 | + end |
| 119 | + |
64 | 120 | it 'does not create new project' do |
65 | 121 | expect { create_remix }.not_to change(Project, :count) |
66 | 122 | end |
67 | 123 | end |
68 | | - end |
69 | 124 |
|
70 | | - def component_array_props(components) |
71 | | - components.map do |x| |
72 | | - { |
73 | | - name: x.name, |
74 | | - content: x.content, |
75 | | - extension: x.extension, |
76 | | - index: x.index |
77 | | - } |
| 125 | + context 'when project components are invalid' do |
| 126 | + let(:invalid_component_params) { { name: 'added_component', extension: 'py', content: '' } } |
| 127 | + |
| 128 | + before do |
| 129 | + remix_params[:components] << invalid_component_params |
| 130 | + end |
| 131 | + |
| 132 | + it 'returns failure' do |
| 133 | + expect(create_remix.failure?).to eq(true) |
| 134 | + end |
| 135 | + |
| 136 | + it 'sets error message' do |
| 137 | + expect(create_remix[:error]).to eq(I18n.t('errors.project.remixing.cannot_save')) |
| 138 | + end |
78 | 139 | end |
79 | 140 | end |
| 141 | + |
| 142 | + def component_props(component) |
| 143 | + { |
| 144 | + name: component.name, |
| 145 | + content: component.content, |
| 146 | + extension: component.extension, |
| 147 | + index: component.index |
| 148 | + } |
| 149 | + end |
80 | 150 | end |
0 commit comments