|
| 1 | +describe ForestLiana::ApimapSorter do |
| 2 | + describe 'apimap reordering' do |
| 3 | + context 'on a disordered apimap' do |
| 4 | + apimap = { |
| 5 | + 'meta': { |
| 6 | + 'orm_version': '4.34.9', |
| 7 | + 'liana_version': '1.5.24', |
| 8 | + 'database_type': 'postgresql', |
| 9 | + liana: 'forest-rails', |
| 10 | + }, |
| 11 | + 'data': [{ |
| 12 | + id: 'users', |
| 13 | + type: 'collections', |
| 14 | + attributes: { |
| 15 | + fields: [ |
| 16 | + { field: 'id', type: 'Number' }, |
| 17 | + { field: 'name', type: 'String' }, |
| 18 | + { field: 'firstName', type: 'String' }, |
| 19 | + { field: 'lastName', type: 'String' }, |
| 20 | + { field: 'email', type: 'String' }, |
| 21 | + { field: 'url', type: 'String' }, |
| 22 | + { field: 'createdAt', type: 'Date' }, |
| 23 | + { field: 'updatedAt', type: 'Date' }, |
| 24 | + ], |
| 25 | + name: 'users', |
| 26 | + } |
| 27 | + }, { |
| 28 | + id: 'guests', |
| 29 | + type: 'collections', |
| 30 | + attributes: { |
| 31 | + fields: [ |
| 32 | + { field: 'id', type: 'Number' }, |
| 33 | + { field: 'email', type: 'String' }, |
| 34 | + { field: 'createdAt', type: 'Date' }, |
| 35 | + { field: 'updatedAt', type: 'Date' }, |
| 36 | + ], |
| 37 | + name: 'guests', |
| 38 | + } |
| 39 | + }, { |
| 40 | + type: 'collections', |
| 41 | + id: 'animals', |
| 42 | + attributes: { |
| 43 | + fields: [ |
| 44 | + { 'is-sortable': false, field: 'id', 'is-filterable': false, type: 'Number' }, |
| 45 | + { type: 'Date', field: 'createdAt' }, |
| 46 | + { field: 'updatedAt', type: 'Date' }, |
| 47 | + ], |
| 48 | + name: 'animals', |
| 49 | + integration: 'close.io', |
| 50 | + 'is-virtual': true, |
| 51 | + } |
| 52 | + }], |
| 53 | + 'included': [{ |
| 54 | + id: 'users.Women', |
| 55 | + type: 'segments', |
| 56 | + attributes: { |
| 57 | + name: 'Women' |
| 58 | + } |
| 59 | + }, { |
| 60 | + id: 'users.import', |
| 61 | + type: 'actions', |
| 62 | + links: { |
| 63 | + self: '/actions' |
| 64 | + }, |
| 65 | + attributes: { |
| 66 | + name: 'import', |
| 67 | + fields: [{ |
| 68 | + isRequired: true, |
| 69 | + type: 'Boolean', |
| 70 | + field: 'Save', |
| 71 | + description: 'save the import file if true.', |
| 72 | + defaultValue: 'true' |
| 73 | + }, { |
| 74 | + type: 'File', |
| 75 | + field: 'File' |
| 76 | + }], |
| 77 | + 'http-method': nil |
| 78 | + } |
| 79 | + }, { |
| 80 | + attributes: { |
| 81 | + name: 'Men' |
| 82 | + }, |
| 83 | + id: 'users.Men', |
| 84 | + type: 'segments' |
| 85 | + }, { |
| 86 | + id: 'animals.ban', |
| 87 | + type: 'actions', |
| 88 | + links: { |
| 89 | + self: '/actions' |
| 90 | + }, |
| 91 | + attributes: { |
| 92 | + name: 'import', |
| 93 | + global: true, |
| 94 | + download: nil, |
| 95 | + endpoint: nil, |
| 96 | + redirect: nil, |
| 97 | + 'http-method': nil |
| 98 | + } |
| 99 | + }] |
| 100 | + } |
| 101 | + |
| 102 | + apimap = ActiveSupport::JSON.encode(apimap) |
| 103 | + apimap = ActiveSupport::JSON.decode(apimap) |
| 104 | + apimap_sorted = ForestLiana::ApimapSorter.new(apimap).perform |
| 105 | + |
| 106 | + it 'should sort the apimap sections' do |
| 107 | + expect(apimap_sorted.keys).to eq(['data', 'included', 'meta']) |
| 108 | + end |
| 109 | + |
| 110 | + it 'should sort the data collections' do |
| 111 | + expect(apimap_sorted['data'].map { |collection| collection['id'] }).to eq( |
| 112 | + ['animals', 'guests', 'users']) |
| 113 | + end |
| 114 | + |
| 115 | + it 'should sort the data collection values' do |
| 116 | + expect(apimap_sorted['data'][0].keys).to eq(['type', 'id', 'attributes']) |
| 117 | + expect(apimap_sorted['data'][1].keys).to eq(['type', 'id', 'attributes']) |
| 118 | + expect(apimap_sorted['data'][2].keys).to eq(['type', 'id', 'attributes']) |
| 119 | + end |
| 120 | + |
| 121 | + it 'should sort the data collections attributes values' do |
| 122 | + expect(apimap_sorted['data'][0]['attributes'].keys).to eq(['name', 'integration', 'is-virtual', 'fields']) |
| 123 | + expect(apimap_sorted['data'][1]['attributes'].keys).to eq(['name', 'fields']) |
| 124 | + expect(apimap_sorted['data'][2]['attributes'].keys).to eq(['name', 'fields']) |
| 125 | + end |
| 126 | + |
| 127 | + it 'should sort the data collections attributes fields by name' do |
| 128 | + expect(apimap_sorted['data'][0]['attributes']['fields'].map { |field| field['field'] }).to eq(['createdAt', 'id', 'updatedAt']) |
| 129 | + expect(apimap_sorted['data'][1]['attributes']['fields'].map { |field| field['field'] }).to eq(['createdAt', 'email', 'id', 'updatedAt']) |
| 130 | + expect(apimap_sorted['data'][2]['attributes']['fields'].map { |field| field['field'] }).to eq(['createdAt', 'email', 'firstName', 'id', 'lastName', 'name', 'updatedAt', 'url']) |
| 131 | + end |
| 132 | + |
| 133 | + it 'should sort the data collections attributes fields values' do |
| 134 | + expect(apimap_sorted['data'][0]['attributes']['fields'][1].keys).to eq(['field', 'type', 'is-filterable', 'is-sortable']) |
| 135 | + end |
| 136 | + |
| 137 | + it 'should sort the included actions and segments objects' do |
| 138 | + expect(apimap_sorted['included'].map { |object| object['id'] }).to eq( |
| 139 | + ['animals.ban', 'users.import', 'users.Men', 'users.Women']) |
| 140 | + end |
| 141 | + |
| 142 | + it 'should sort the included actions and segments objects values' do |
| 143 | + expect(apimap_sorted['included'][0].keys).to eq(['type', 'id', 'attributes', 'links']) |
| 144 | + expect(apimap_sorted['included'][1].keys).to eq(['type', 'id', 'attributes', 'links']) |
| 145 | + expect(apimap_sorted['included'][2].keys).to eq(['type', 'id', 'attributes']) |
| 146 | + expect(apimap_sorted['included'][3].keys).to eq(['type', 'id', 'attributes']) |
| 147 | + end |
| 148 | + |
| 149 | + it 'should sort the included actions and segments objects attributes values' do |
| 150 | + expect(apimap_sorted['included'][0]['attributes'].keys).to eq(['name', 'download', 'endpoint', 'global', 'http-method', 'redirect']) |
| 151 | + expect(apimap_sorted['included'][1]['attributes'].keys).to eq(['name', 'http-method', 'fields']) |
| 152 | + expect(apimap_sorted['included'][2]['attributes'].keys).to eq(['name']) |
| 153 | + expect(apimap_sorted['included'][3]['attributes'].keys).to eq(['name']) |
| 154 | + end |
| 155 | + |
| 156 | + it 'should sort the included action attributes fields by name' do |
| 157 | + expect(apimap_sorted['included'][1]['attributes']['fields'].map { |field| field['field'] }).to eq(['File', 'Save']) |
| 158 | + end |
| 159 | + |
| 160 | + it 'should sort the included action fields values' do |
| 161 | + expect(apimap_sorted['included'][1]['attributes']['fields'][1].keys).to eq(['field', 'type', 'defaultValue', 'description', 'isRequired']) |
| 162 | + end |
| 163 | + |
| 164 | + it 'should sort the meta values' do |
| 165 | + expect(apimap_sorted['meta'].keys).to eq( |
| 166 | + ['database_type', 'liana', 'liana_version', 'orm_version']) |
| 167 | + end |
| 168 | + end |
| 169 | + end |
| 170 | +end |
0 commit comments