diff --git a/ui/src/components/AircraftModelFilter.vue b/ui/src/components/AircraftModelFilter.vue index 3dbda59..6dd6cde 100644 --- a/ui/src/components/AircraftModelFilter.vue +++ b/ui/src/components/AircraftModelFilter.vue @@ -28,6 +28,12 @@ export default { components: { Multiselect }, + props: { + models: { + type: Object, + default: null + } + }, data() { return { aircraft_type: null, @@ -39,6 +45,26 @@ export default { watch: { $route() { + this.parseAndApplyRoute() + }, + + selected(val) { + if (val == null) { + return + } + this.$router.push({ + name: 'offer_details', + params: { aircraftType: val.aircraft_type, manufacturer: val.manufacturer, model: val.model } + }) + } + }, + + created() { + this.loadAircraftTypes() + }, + + methods: { + parseAndApplyRoute() { const pathSegments = this.$route.path.split('/') switch (pathSegments[1]) { case 'glider': @@ -65,26 +91,18 @@ export default { this.updateAircraftTypes() } }, + loadAircraftTypes() { + if (this.models) { + this.all_aircraft_types = this.models - selected(val) { - if (val == null) { + this.parseAndApplyRoute() + this.updateAircraftTypes() return } - this.$router.push({ - name: 'offer_details', - params: { aircraftType: val.aircraft_type, manufacturer: val.manufacturer, model: val.model } - }) - } - }, - created() { - this.loadAircraftTypes() - }, - - methods: { - loadAircraftTypes() { axios.get(`/models`).then((response) => { this.all_aircraft_types = response.data + this.parseAndApplyRoute() this.updateAircraftTypes() }) }, diff --git a/ui/src/components/__tests__/AircraftModelFilter.spec.js b/ui/src/components/__tests__/AircraftModelFilter.spec.js new file mode 100644 index 0000000..5248679 --- /dev/null +++ b/ui/src/components/__tests__/AircraftModelFilter.spec.js @@ -0,0 +1,58 @@ +import { describe, it, expect } from 'vitest' +import { mount } from '@vue/test-utils' +import AircraftModelFilter from '../AircraftModelFilter.vue' + +const models = { + AlexanderSchleicher: { + models: { + glider: ['ASW 15', 'ASW 17', 'ASW 19', 'ASW 20'], + tmg: ['ASK 14', 'ASK 16'] + } + }, + Antonov: { + models: { + airplane: ['An-2', 'An-225 Mriya'] + } + } +} + +const mockRouter = { + push: function () {} +} +const mockRoute = { + path: '/' +} + +function mocksWithPath(path) { + mockRoute.path = path + return { + mocks: { + $route: mockRoute, + $router: mockRouter + } + } +} + +describe('AircraftModelFilter', () => { + it('should list all categories for homepage', () => { + const wrapper = mount(AircraftModelFilter, { + props: { models: models }, + global: mocksWithPath('/') + }) + + expect(wrapper.text()) + .toContain('AlexanderSchleicher', 'ASW 15', 'ASK 16') // both glider and tmg + .toContain('Antonov', 'An-2', 'An-225 Mriya') // airplanes + }) + + it('should list only gliders on /glider', () => { + const wrapper = mount(AircraftModelFilter, { + props: { models: models }, + global: mocksWithPath('/glider') + }) + + expect(wrapper.text()) + .toContain('AlexanderSchleicher', 'ASW 15') + .not.toContain('ASK 16', 'Antonov', 'An-2', 'An-225 Mriya') + }) +}) diff --git a/ui/src/components/__tests__/OfferThumb.spec.js b/ui/src/components/__tests__/OfferThumb.spec.js index 0d23108..393ccb1 100644 --- a/ui/src/components/__tests__/OfferThumb.spec.js +++ b/ui/src/components/__tests__/OfferThumb.spec.js @@ -25,6 +25,6 @@ describe('OfferThumb', () => { const wrapper = mount(OfferThumb, { props: { offer: offer } }) - expect(wrapper.text()).contains('Rolladen Schneider LS4').contains('2020-02-26') + expect(wrapper.text()).toContain('Rolladen Schneider LS4', '2020-02-26') }) })