From 49100f8c6484cec0635f166daed89cb9f024179d Mon Sep 17 00:00:00 2001 From: Maciej Barelkowski Date: Tue, 24 Sep 2019 11:43:40 +0200 Subject: [PATCH] feat(deploy): derive default deployment name from the filename --- .../plugins/deployment-tool/DeploymentTool.js | 20 ++- .../__tests__/DeploymentToolSpec.js | 137 ++++++++++++++++++ 2 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 client/src/plugins/deployment-tool/__tests__/DeploymentToolSpec.js diff --git a/client/src/plugins/deployment-tool/DeploymentTool.js b/client/src/plugins/deployment-tool/DeploymentTool.js index 6ec8d0f0fe..0ac45eb936 100644 --- a/client/src/plugins/deployment-tool/DeploymentTool.js +++ b/client/src/plugins/deployment-tool/DeploymentTool.js @@ -130,8 +130,10 @@ export default class DeploymentTool extends PureComponent { } getDetailsFromUserInput(tab, details) { + const initialDetails = this.getInitialDetails(tab, details); + return new Promise(resolve => { - const handleClose = (result) => { + const handleClose = result => { this.setState({ modalState: null @@ -151,7 +153,7 @@ export default class DeploymentTool extends PureComponent { this.setState({ modalState: { tab, - details, + details: initialDetails, handleClose } }); @@ -187,6 +189,16 @@ export default class DeploymentTool extends PureComponent { return connectionError; } + getInitialDetails(tab, providedDetails) { + const details = { ...providedDetails }; + + if (!details.deploymentName) { + details.deploymentName = withoutExtension(tab.name); + } + + return details; + } + getValidatedFields(values) { const fields = Object.keys(values); @@ -286,3 +298,7 @@ export default class DeploymentTool extends PureComponent { function isFocusedOnInput(event) { return event.type === 'focus' && ['INPUT', 'TEXTAREA'].includes(event.target.tagName); } + +function withoutExtension(name) { + return name.replace(/\.[^.]+$/, ''); +} diff --git a/client/src/plugins/deployment-tool/__tests__/DeploymentToolSpec.js b/client/src/plugins/deployment-tool/__tests__/DeploymentToolSpec.js new file mode 100644 index 0000000000..18980b3ad0 --- /dev/null +++ b/client/src/plugins/deployment-tool/__tests__/DeploymentToolSpec.js @@ -0,0 +1,137 @@ +/** + * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH + * under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. + * + * Camunda licenses this file to you under the MIT; you may not use this file + * except in compliance with the MIT License. + */ + +/* global sinon */ + +import React from 'react'; + +import { mount, shallow } from 'enzyme'; + +import DeploymentTool from '../DeploymentTool'; +import DeploymentDetailsModal from '../DeploymentDetailsModal'; + + +describe('', () => { + + it('should render', () => { + createDeploymentTool(); + }); + + + it('should derive the default deployment name from filename', () => { + + // given + const { instance } = createDeploymentTool(); + + // when + const details = instance.getInitialDetails({ name }); + + // then + expect(details).to.have.property('deploymentName', name); + }); + + + describe('#deploy', () => { + + let fetchStub, + mounted; + + beforeEach(() => { + fetchStub = sinon.stub(window, 'fetch'); + }); + + afterEach(() => { + fetchStub.restore(); + + if (mounted) { + mounted.unmount(); + } + }); + + + it('should derive deployment name from filename', async () => { + + // given + const activeTab = createTab({ name: 'foo.bpmn' }); + const { + wrapper, + instance + } = createDeploymentTool({ activeTab }, mount); + + mounted = wrapper; + + // when + instance.deploy(); + + await nextTick(); + wrapper.update(); + + // then + const modal = wrapper.find(DeploymentDetailsModal).first(); + + const onClose = modal.prop('onClose'); + const deploymentName = modal.find('input[name="deploymentName"]').first().getDOMNode().value; + + expect(deploymentName).to.eql('foo'); + + onClose(); + }); + + }); +}); + + + +// helper //// +function createDeploymentTool({ + activeTab = createTab(), + ...props +} = {}, render = shallow) { + const subscribe = (event, callback) => { + event === 'app.activeTabChanged' && callback(activeTab); + }; + + const triggerAction = event => { + switch (event) { + case 'save': + return activeTab; + } + }; + + const wrapper = render(); + + return { + wrapper, + instance: wrapper.instance() + }; +} + +function createTab(overrides = {}) { + return { + id: 42, + name: 'foo.bar', + type: 'bar', + title: 'unsaved', + file: { + name: 'foo.bar', + contents: '', + path: null + }, + ...overrides + }; +} + +function nextTick() { + return new Promise(resolve => process.nextTick(() => resolve())); +} \ No newline at end of file