From fd6ad924d998eb7cdc7d2f42772efc8f6ada767f Mon Sep 17 00:00:00 2001 From: tomdds Date: Thu, 11 Jul 2019 17:36:56 -0400 Subject: [PATCH 1/6] Spike that generates a checkbox template from jinja and allows us to mount and manipulate it --- .../__tests__/checkbox_input.test.js | 68 +++++++++++++++++++ .../checkbox_input_template.html | 21 ++++++ script/render_vue_component.py | 25 +++++++ 3 files changed, 114 insertions(+) create mode 100644 js/components/__tests__/checkbox_input.test.js create mode 100644 js/test_templates/checkbox_input_template.html create mode 100644 script/render_vue_component.py diff --git a/js/components/__tests__/checkbox_input.test.js b/js/components/__tests__/checkbox_input.test.js new file mode 100644 index 00000000..6c3b69c5 --- /dev/null +++ b/js/components/__tests__/checkbox_input.test.js @@ -0,0 +1,68 @@ +import { mount } from '@vue/test-utils' + +import Vue from 'vue' + +import checkboxinput from '../checkbox_input' + +const fs = require('fs') +const testTemplate = fs.readFileSync('js/test_templates/checkbox_input_template.html', 'utf-8') +console.log(testTemplate) +const template = ` +
+
+
+ + + Some words about this checkbox + +
+
+
+` + +// const templatedCheckboxInput = { +// ...checkboxinput, +// template +// } + +// Define test wrapping component +// Use test template for wrapping component template +// Inject component under test (checkboxInput) into wrapping component?? + +const WrapperComponent = { + name: 'WrapperComponent', + components: { checkboxinput }, + template: testTemplate, + data: function () { + return { + name: 'testCheck', + initialChecked: true + } + } +} + +describe('CheckboxInput Renders Correctly', () => { + it('Should initialize checked', () => { + const wrapper = mount(WrapperComponent, { + propsData: { + name: 'testCheck', + initialChecked: true + } + }) + + wrapper.vm.$children[0].$data.isChecked = true + + expect(wrapper.find('.usa-input input').element.checked).toBe(true) + }) + + it('Should initialize unchecked', () => { + const wrapper = mount(WrapperComponent, { + propsData: { + name: 'testCheck', + initialChecked: false + } + }) + + expect(wrapper.find('.usa-input input').element.checked).toBe(false) + }) +}) diff --git a/js/test_templates/checkbox_input_template.html b/js/test_templates/checkbox_input_template.html new file mode 100644 index 00000000..097b9da1 --- /dev/null +++ b/js/test_templates/checkbox_input_template.html @@ -0,0 +1,21 @@ + +
+
+ +
+ + + + + + +
+
+ +
+
\ No newline at end of file diff --git a/script/render_vue_component.py b/script/render_vue_component.py new file mode 100644 index 00000000..e13367d8 --- /dev/null +++ b/script/render_vue_component.py @@ -0,0 +1,25 @@ +from jinja2 import Environment, FileSystemLoader +from wtforms.widgets import CheckboxInput +from wtforms.fields import BooleanField +from wtforms import Form + +env = Environment(loader=FileSystemLoader('templates/components')) + +checkbox_template = env.get_template('checkbox_input.html') + +field = BooleanField( + label="Hooray!", + default=False, + widget=CheckboxInput() +) + +class BoolForm(Form): + testVal = field + +ci_macro = getattr(checkbox_template.module, 'CheckboxInput') + +output_from_parsed_template = ci_macro(BoolForm().testVal) +print(output_from_parsed_template) + +with open("js/test_templates/checkbox_input_template.html", "w") as fh: + fh.write(output_from_parsed_template) From 5234677ad356d63d2f3d829e950d6a0792e7d5fb Mon Sep 17 00:00:00 2001 From: tomdds Date: Thu, 11 Jul 2019 17:52:54 -0400 Subject: [PATCH 2/6] Set incoming props as variable names rather than values in test rendering to allow props to be properly passed to component under test --- js/components/__tests__/checkbox_input.test.js | 7 +++++-- js/test_templates/checkbox_input_template.html | 4 ++-- script/render_vue_component.py | 6 +++--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/js/components/__tests__/checkbox_input.test.js b/js/components/__tests__/checkbox_input.test.js index 6c3b69c5..baa0907c 100644 --- a/js/components/__tests__/checkbox_input.test.js +++ b/js/components/__tests__/checkbox_input.test.js @@ -33,10 +33,13 @@ const WrapperComponent = { name: 'WrapperComponent', components: { checkboxinput }, template: testTemplate, + props: { + initialChecked: Boolean, + }, data: function () { return { name: 'testCheck', - initialChecked: true + initialchecked: this.initialChecked } } } @@ -50,7 +53,7 @@ describe('CheckboxInput Renders Correctly', () => { } }) - wrapper.vm.$children[0].$data.isChecked = true + // wrapper.vm.$children[0].$data.isChecked = true expect(wrapper.find('.usa-input input').element.checked).toBe(true) }) diff --git a/js/test_templates/checkbox_input_template.html b/js/test_templates/checkbox_input_template.html index 097b9da1..7d46be17 100644 --- a/js/test_templates/checkbox_input_template.html +++ b/js/test_templates/checkbox_input_template.html @@ -2,14 +2,14 @@ name='testVal' inline-template key='testVal' - v-bind:initial-checked='false' + v-bind:initial-checked='initialchecked' >
- + diff --git a/script/render_vue_component.py b/script/render_vue_component.py index e13367d8..9660a86f 100644 --- a/script/render_vue_component.py +++ b/script/render_vue_component.py @@ -1,15 +1,15 @@ from jinja2 import Environment, FileSystemLoader from wtforms.widgets import CheckboxInput -from wtforms.fields import BooleanField +from wtforms.fields import StringField from wtforms import Form env = Environment(loader=FileSystemLoader('templates/components')) checkbox_template = env.get_template('checkbox_input.html') -field = BooleanField( +field = StringField( label="Hooray!", - default=False, + default="initialchecked", widget=CheckboxInput() ) From 38a01f7db39a54da1da4afacb91cbbc614320fc3 Mon Sep 17 00:00:00 2001 From: tomdds Date: Fri, 12 Jul 2019 12:05:54 -0400 Subject: [PATCH 3/6] Clean up and generalize jinja to vue test template rendering and testing --- .../__tests__/checkbox_input.test.js | 53 +++-------------- js/components/__tests__/upload_input.test.js | 49 +++++++++++++++ .../checkbox_input_template.html | 10 ++-- .../upload_input_error_template.html | 49 +++++++++++++++ js/test_templates/upload_input_template.html | 40 +++++++++++++ js/test_utils/component_test_helpers.js | 39 ++++++++++++ script/render_vue_component.py | 59 ++++++++++++++----- 7 files changed, 233 insertions(+), 66 deletions(-) create mode 100644 js/components/__tests__/upload_input.test.js create mode 100644 js/test_templates/upload_input_error_template.html create mode 100644 js/test_templates/upload_input_template.html create mode 100644 js/test_utils/component_test_helpers.js diff --git a/js/components/__tests__/checkbox_input.test.js b/js/components/__tests__/checkbox_input.test.js index baa0907c..dd4d9d42 100644 --- a/js/components/__tests__/checkbox_input.test.js +++ b/js/components/__tests__/checkbox_input.test.js @@ -1,60 +1,24 @@ import { mount } from '@vue/test-utils' -import Vue from 'vue' - import checkboxinput from '../checkbox_input' -const fs = require('fs') -const testTemplate = fs.readFileSync('js/test_templates/checkbox_input_template.html', 'utf-8') -console.log(testTemplate) -const template = ` -
-
-
- - - Some words about this checkbox - -
-
-
-` +import { makeTestWrapper } from '../../test_utils/component_test_helpers' -// const templatedCheckboxInput = { -// ...checkboxinput, -// template -// } - -// Define test wrapping component -// Use test template for wrapping component template -// Inject component under test (checkboxInput) into wrapping component?? - -const WrapperComponent = { - name: 'WrapperComponent', - components: { checkboxinput }, - template: testTemplate, - props: { - initialChecked: Boolean, +const WrapperComponent = makeTestWrapper({ + components: { + checkboxinput }, - data: function () { - return { - name: 'testCheck', - initialchecked: this.initialChecked - } - } -} + templatePath: 'checkbox_input_template.html', +}) describe('CheckboxInput Renders Correctly', () => { it('Should initialize checked', () => { const wrapper = mount(WrapperComponent, { propsData: { name: 'testCheck', - initialChecked: true + initialData: true } }) - - // wrapper.vm.$children[0].$data.isChecked = true - expect(wrapper.find('.usa-input input').element.checked).toBe(true) }) @@ -62,10 +26,9 @@ describe('CheckboxInput Renders Correctly', () => { const wrapper = mount(WrapperComponent, { propsData: { name: 'testCheck', - initialChecked: false + initialData: false } }) - expect(wrapper.find('.usa-input input').element.checked).toBe(false) }) }) diff --git a/js/components/__tests__/upload_input.test.js b/js/components/__tests__/upload_input.test.js new file mode 100644 index 00000000..a5c4cbeb --- /dev/null +++ b/js/components/__tests__/upload_input.test.js @@ -0,0 +1,49 @@ +import { mount } from '@vue/test-utils' + +import uploadinput from '../upload_input' + +import { makeTestWrapper } from '../../test_utils/component_test_helpers' + +const UploadWrapper = makeTestWrapper({ + components: { uploadinput }, + templatePath: 'upload_input_template.html' +}) + +const UploadErrorWrapper = makeTestWrapper({ + components: { uploadinput }, + templatePath: 'upload_input_error_template.html' +}) + +describe('UploadInput Test', () => { + it('should show input and button when no attachment present', () => { + const wrapper = mount(UploadWrapper, { + propsData: { + initialData: null + } + }) + + const fileInput = wrapper.find('input[type=file]').element + expect(fileInput).not.toBe(undefined) + }) + + it('should show file name and hide input', () => { + const wrapper = mount(UploadWrapper, { + propsData: { + initialData: 'somepdf.pdf' + } + }) + + const fileInput = wrapper.find('input[type=file]').element + const fileNameSpan = wrapper.find('.uploaded-file__name') + + expect(fileInput).toBe(undefined) + expect(fileNameSpan.html()).toContain('somepdf.pdf') + }) + + it('should correctly display error treatment', () => { + const wrapper = mount(UploadErrorWrapper) + + const messageArea = wrapper.find('.usa-input__message') + expect(messageArea.html()).toContain('Test Error Message') + }) +}) diff --git a/js/test_templates/checkbox_input_template.html b/js/test_templates/checkbox_input_template.html index 7d46be17..f5dd7474 100644 --- a/js/test_templates/checkbox_input_template.html +++ b/js/test_templates/checkbox_input_template.html @@ -1,16 +1,16 @@
- - + + diff --git a/js/test_templates/upload_input_error_template.html b/js/test_templates/upload_input_error_template.html new file mode 100644 index 00000000..077eaeb2 --- /dev/null +++ b/js/test_templates/upload_input_error_template.html @@ -0,0 +1,49 @@ + +
+
+ + + + + Remove +
+
+ + +
+ + +
+ + Test Error Message + +
+
+
\ No newline at end of file diff --git a/js/test_templates/upload_input_template.html b/js/test_templates/upload_input_template.html new file mode 100644 index 00000000..2179385f --- /dev/null +++ b/js/test_templates/upload_input_template.html @@ -0,0 +1,40 @@ + +
+
+ + + + + Remove +
+
+ + +
+ + +
+ +
+
+
\ No newline at end of file diff --git a/js/test_utils/component_test_helpers.js b/js/test_utils/component_test_helpers.js new file mode 100644 index 00000000..4339e81f --- /dev/null +++ b/js/test_utils/component_test_helpers.js @@ -0,0 +1,39 @@ +const fs = require('fs') + +/* +Jinja templates will be exported with a root node that is a custom tag mapped to a vue component +This wrapper allows us to mount the template and provide props. Props are provided by setting +values on in this wrapper's data function. The names returned by the data function must align +with the field names set in the template exporter script. + +In the example below, initial-checked is set to a value 'initialchecked', so +'initialchecked' must be exported as a key from from the data function in order +to be passed as a prop to checkboxinput at mount time + +*/ +const makeTestWrapper = ({components, templatePath}) => { + const templateString = fs.readFileSync(`js/test_templates/${templatePath}`, 'utf-8') + + const WrapperComponent = { + name: 'WrapperComponent', + components, + template: templateString, + props: ['initialData'], + data: function() { + return { + initialvalue: this.initialData + } + } + } + + return WrapperComponent +} + +export { + makeTestWrapper +} diff --git a/script/render_vue_component.py b/script/render_vue_component.py index 9660a86f..11e776d5 100644 --- a/script/render_vue_component.py +++ b/script/render_vue_component.py @@ -1,25 +1,52 @@ +import os +import sys + +parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) +sys.path.append(parent_dir) + from jinja2 import Environment, FileSystemLoader -from wtforms.widgets import CheckboxInput -from wtforms.fields import StringField +from wtforms.widgets import CheckboxInput, FileInput +from wtforms.fields import StringField, FileField +from wtforms.validators import InputRequired from wtforms import Form -env = Environment(loader=FileSystemLoader('templates/components')) +from atst.filters import iconSvg -checkbox_template = env.get_template('checkbox_input.html') +env = Environment(loader=FileSystemLoader('templates')) -field = StringField( - label="Hooray!", - default="initialchecked", - widget=CheckboxInput() -) +env.filters['iconSvg'] = iconSvg -class BoolForm(Form): - testVal = field +# override tojson as identity function to prevent +# wrapping strings in extra quotes +env.filters['tojson'] = lambda x: x +class InitialValueForm(Form): + datafield = StringField( + label="initialvalue value", + default="initialvalue" + ) + + errorfield = StringField( + label="error", + validators=[InputRequired(message="Test Error Message")] + ) + +checkbox_template = env.get_template('components/checkbox_input.html') ci_macro = getattr(checkbox_template.module, 'CheckboxInput') - -output_from_parsed_template = ci_macro(BoolForm().testVal) -print(output_from_parsed_template) - +checkbox_input_form = InitialValueForm() +checkbox_input_form.datafield.widget = CheckboxInput() +rendered_checkbox_macro = ci_macro(checkbox_input_form.datafield) with open("js/test_templates/checkbox_input_template.html", "w") as fh: - fh.write(output_from_parsed_template) + fh.write(rendered_checkbox_macro) + +upload_template = env.get_template('components/upload_input.html') +up_macro = getattr(upload_template.module, 'UploadInput') +rendered_upload_macro = up_macro(InitialValueForm().datafield) +with open("js/test_templates/upload_input_template.html", "w") as fh: + fh.write(rendered_upload_macro) + +erroredform = InitialValueForm() +erroredform.validate() +rendered_upload_error_macro = up_macro(erroredform.errorfield) +with open("js/test_templates/upload_input_error_template.html", "w") as fh: + fh.write(rendered_upload_error_macro) From 0682411954912506ec5f667cae9e470eed778249 Mon Sep 17 00:00:00 2001 From: tomdds Date: Fri, 12 Jul 2019 12:13:22 -0400 Subject: [PATCH 4/6] Fix formatting --- .../__tests__/checkbox_input.test.js | 40 ++++++------- js/components/__tests__/upload_input.test.js | 58 +++++++++---------- js/test_utils/component_test_helpers.js | 35 +++++------ script/render_vue_component.py | 24 ++++---- 4 files changed, 78 insertions(+), 79 deletions(-) diff --git a/js/components/__tests__/checkbox_input.test.js b/js/components/__tests__/checkbox_input.test.js index dd4d9d42..6be9c1e4 100644 --- a/js/components/__tests__/checkbox_input.test.js +++ b/js/components/__tests__/checkbox_input.test.js @@ -5,30 +5,30 @@ import checkboxinput from '../checkbox_input' import { makeTestWrapper } from '../../test_utils/component_test_helpers' const WrapperComponent = makeTestWrapper({ - components: { - checkboxinput - }, - templatePath: 'checkbox_input_template.html', + components: { + checkboxinput, + }, + templatePath: 'checkbox_input_template.html', }) describe('CheckboxInput Renders Correctly', () => { - it('Should initialize checked', () => { - const wrapper = mount(WrapperComponent, { - propsData: { - name: 'testCheck', - initialData: true - } - }) - expect(wrapper.find('.usa-input input').element.checked).toBe(true) + it('Should initialize checked', () => { + const wrapper = mount(WrapperComponent, { + propsData: { + name: 'testCheck', + initialData: true, + }, }) + expect(wrapper.find('.usa-input input').element.checked).toBe(true) + }) - it('Should initialize unchecked', () => { - const wrapper = mount(WrapperComponent, { - propsData: { - name: 'testCheck', - initialData: false - } - }) - expect(wrapper.find('.usa-input input').element.checked).toBe(false) + it('Should initialize unchecked', () => { + const wrapper = mount(WrapperComponent, { + propsData: { + name: 'testCheck', + initialData: false, + }, }) + expect(wrapper.find('.usa-input input').element.checked).toBe(false) + }) }) diff --git a/js/components/__tests__/upload_input.test.js b/js/components/__tests__/upload_input.test.js index a5c4cbeb..96a1be89 100644 --- a/js/components/__tests__/upload_input.test.js +++ b/js/components/__tests__/upload_input.test.js @@ -5,45 +5,45 @@ import uploadinput from '../upload_input' import { makeTestWrapper } from '../../test_utils/component_test_helpers' const UploadWrapper = makeTestWrapper({ - components: { uploadinput }, - templatePath: 'upload_input_template.html' + components: { uploadinput }, + templatePath: 'upload_input_template.html', }) const UploadErrorWrapper = makeTestWrapper({ - components: { uploadinput }, - templatePath: 'upload_input_error_template.html' + components: { uploadinput }, + templatePath: 'upload_input_error_template.html', }) describe('UploadInput Test', () => { - it('should show input and button when no attachment present', () => { - const wrapper = mount(UploadWrapper, { - propsData: { - initialData: null - } - }) - - const fileInput = wrapper.find('input[type=file]').element - expect(fileInput).not.toBe(undefined) + it('should show input and button when no attachment present', () => { + const wrapper = mount(UploadWrapper, { + propsData: { + initialData: null, + }, }) - it('should show file name and hide input', () => { - const wrapper = mount(UploadWrapper, { - propsData: { - initialData: 'somepdf.pdf' - } - }) + const fileInput = wrapper.find('input[type=file]').element + expect(fileInput).not.toBe(undefined) + }) - const fileInput = wrapper.find('input[type=file]').element - const fileNameSpan = wrapper.find('.uploaded-file__name') - - expect(fileInput).toBe(undefined) - expect(fileNameSpan.html()).toContain('somepdf.pdf') + it('should show file name and hide input', () => { + const wrapper = mount(UploadWrapper, { + propsData: { + initialData: 'somepdf.pdf', + }, }) - it('should correctly display error treatment', () => { - const wrapper = mount(UploadErrorWrapper) + const fileInput = wrapper.find('input[type=file]').element + const fileNameSpan = wrapper.find('.uploaded-file__name') - const messageArea = wrapper.find('.usa-input__message') - expect(messageArea.html()).toContain('Test Error Message') - }) + expect(fileInput).toBe(undefined) + expect(fileNameSpan.html()).toContain('somepdf.pdf') + }) + + it('should correctly display error treatment', () => { + const wrapper = mount(UploadErrorWrapper) + + const messageArea = wrapper.find('.usa-input__message') + expect(messageArea.html()).toContain('Test Error Message') + }) }) diff --git a/js/test_utils/component_test_helpers.js b/js/test_utils/component_test_helpers.js index 4339e81f..e81d9603 100644 --- a/js/test_utils/component_test_helpers.js +++ b/js/test_utils/component_test_helpers.js @@ -16,24 +16,25 @@ to be passed as a prop to checkboxinput at mount time v-bind:initial-checked='initialvalue' > */ -const makeTestWrapper = ({components, templatePath}) => { - const templateString = fs.readFileSync(`js/test_templates/${templatePath}`, 'utf-8') +const makeTestWrapper = ({ components, templatePath }) => { + const templateString = fs.readFileSync( + `js/test_templates/${templatePath}`, + 'utf-8' + ) - const WrapperComponent = { - name: 'WrapperComponent', - components, - template: templateString, - props: ['initialData'], - data: function() { - return { - initialvalue: this.initialData - } - } - } + const WrapperComponent = { + name: 'WrapperComponent', + components, + template: templateString, + props: ['initialData'], + data: function() { + return { + initialvalue: this.initialData, + } + }, + } - return WrapperComponent + return WrapperComponent } -export { - makeTestWrapper -} +export { makeTestWrapper } diff --git a/script/render_vue_component.py b/script/render_vue_component.py index 11e776d5..989afb9e 100644 --- a/script/render_vue_component.py +++ b/script/render_vue_component.py @@ -12,35 +12,33 @@ from wtforms import Form from atst.filters import iconSvg -env = Environment(loader=FileSystemLoader('templates')) +env = Environment(loader=FileSystemLoader("templates")) -env.filters['iconSvg'] = iconSvg +env.filters["iconSvg"] = iconSvg # override tojson as identity function to prevent # wrapping strings in extra quotes -env.filters['tojson'] = lambda x: x +env.filters["tojson"] = lambda x: x + class InitialValueForm(Form): - datafield = StringField( - label="initialvalue value", - default="initialvalue" - ) + datafield = StringField(label="initialvalue value", default="initialvalue") errorfield = StringField( - label="error", - validators=[InputRequired(message="Test Error Message")] + label="error", validators=[InputRequired(message="Test Error Message")] ) -checkbox_template = env.get_template('components/checkbox_input.html') -ci_macro = getattr(checkbox_template.module, 'CheckboxInput') + +checkbox_template = env.get_template("components/checkbox_input.html") +ci_macro = getattr(checkbox_template.module, "CheckboxInput") checkbox_input_form = InitialValueForm() checkbox_input_form.datafield.widget = CheckboxInput() rendered_checkbox_macro = ci_macro(checkbox_input_form.datafield) with open("js/test_templates/checkbox_input_template.html", "w") as fh: fh.write(rendered_checkbox_macro) -upload_template = env.get_template('components/upload_input.html') -up_macro = getattr(upload_template.module, 'UploadInput') +upload_template = env.get_template("components/upload_input.html") +up_macro = getattr(upload_template.module, "UploadInput") rendered_upload_macro = up_macro(InitialValueForm().datafield) with open("js/test_templates/upload_input_template.html", "w") as fh: fh.write(rendered_upload_macro) From 506d53f8624ca0a31cf3cfea69ffab8feee8369d Mon Sep 17 00:00:00 2001 From: tomdds Date: Mon, 15 Jul 2019 11:05:42 -0400 Subject: [PATCH 5/6] Migrate Jinja to Vue Template Rendering to pytest --- script/render_vue_component.py | 50 -------------------------------- tests/render_vue_component.py | 52 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 50 deletions(-) delete mode 100644 script/render_vue_component.py create mode 100644 tests/render_vue_component.py diff --git a/script/render_vue_component.py b/script/render_vue_component.py deleted file mode 100644 index 989afb9e..00000000 --- a/script/render_vue_component.py +++ /dev/null @@ -1,50 +0,0 @@ -import os -import sys - -parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) -sys.path.append(parent_dir) - -from jinja2 import Environment, FileSystemLoader -from wtforms.widgets import CheckboxInput, FileInput -from wtforms.fields import StringField, FileField -from wtforms.validators import InputRequired -from wtforms import Form - -from atst.filters import iconSvg - -env = Environment(loader=FileSystemLoader("templates")) - -env.filters["iconSvg"] = iconSvg - -# override tojson as identity function to prevent -# wrapping strings in extra quotes -env.filters["tojson"] = lambda x: x - - -class InitialValueForm(Form): - datafield = StringField(label="initialvalue value", default="initialvalue") - - errorfield = StringField( - label="error", validators=[InputRequired(message="Test Error Message")] - ) - - -checkbox_template = env.get_template("components/checkbox_input.html") -ci_macro = getattr(checkbox_template.module, "CheckboxInput") -checkbox_input_form = InitialValueForm() -checkbox_input_form.datafield.widget = CheckboxInput() -rendered_checkbox_macro = ci_macro(checkbox_input_form.datafield) -with open("js/test_templates/checkbox_input_template.html", "w") as fh: - fh.write(rendered_checkbox_macro) - -upload_template = env.get_template("components/upload_input.html") -up_macro = getattr(upload_template.module, "UploadInput") -rendered_upload_macro = up_macro(InitialValueForm().datafield) -with open("js/test_templates/upload_input_template.html", "w") as fh: - fh.write(rendered_upload_macro) - -erroredform = InitialValueForm() -erroredform.validate() -rendered_upload_error_macro = up_macro(erroredform.errorfield) -with open("js/test_templates/upload_input_error_template.html", "w") as fh: - fh.write(rendered_upload_error_macro) diff --git a/tests/render_vue_component.py b/tests/render_vue_component.py new file mode 100644 index 00000000..c9de4037 --- /dev/null +++ b/tests/render_vue_component.py @@ -0,0 +1,52 @@ +import pytest + +from wtforms.widgets import CheckboxInput +from wtforms.fields import StringField +from wtforms.validators import InputRequired +from wtforms import Form + +class InitialValueForm(Form): + datafield = StringField(label="initialvalue value", default="initialvalue") + + errorfield = StringField( + label="error", validators=[InputRequired(message="Test Error Message")] + ) + +@pytest.fixture +def env(app, scope="function"): + return app.jinja_env + +@pytest.fixture +def upload_input_macro(env): + # override tojson as identity function to prevent + # wrapping strings in extra quotes + env.filters["tojson"] = lambda x: x + upload_template = env.get_template("components/upload_input.html") + return getattr(upload_template.module, "UploadInput") + +@pytest.fixture +def checkbox_input_macro(env): + checkbox_template = env.get_template("components/checkbox_input.html") + return getattr(checkbox_template.module, "CheckboxInput") + +@pytest.fixture +def initial_value_form(scope="function"): + return InitialValueForm() + +def write_template(content, name): + with open("js/test_templates/{}".format(name), "w") as fh: + fh.write(content) + +def test_make_checkbox_input_template(checkbox_input_macro, initial_value_form): + initial_value_form.datafield.widget = CheckboxInput() + rendered_checkbox_macro = checkbox_input_macro(initial_value_form.datafield) + write_template(rendered_checkbox_macro, "checkbox_input_template.html") + +def test_make_upload_input_template(upload_input_macro, initial_value_form): + rendered_upload_macro = upload_input_macro(initial_value_form.datafield) + write_template(rendered_upload_macro, "upload_input_template.html") + +def test_make_upload_input_error_template(upload_input_macro, initial_value_form): + initial_value_form.validate() + rendered_upload_macro = upload_input_macro(initial_value_form.errorfield) + write_template(rendered_upload_macro, "upload_input_error_template.html") From b9bfe64c39f628e78e8a25c6de1f2971141824ed Mon Sep 17 00:00:00 2001 From: tomdds Date: Mon, 15 Jul 2019 14:00:43 -0400 Subject: [PATCH 6/6] Fixed file formatting --- tests/render_vue_component.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/render_vue_component.py b/tests/render_vue_component.py index c9de4037..5d9f3b08 100644 --- a/tests/render_vue_component.py +++ b/tests/render_vue_component.py @@ -5,6 +5,7 @@ from wtforms.fields import StringField from wtforms.validators import InputRequired from wtforms import Form + class InitialValueForm(Form): datafield = StringField(label="initialvalue value", default="initialvalue") @@ -12,10 +13,12 @@ class InitialValueForm(Form): label="error", validators=[InputRequired(message="Test Error Message")] ) + @pytest.fixture def env(app, scope="function"): return app.jinja_env + @pytest.fixture def upload_input_macro(env): # override tojson as identity function to prevent @@ -24,28 +27,34 @@ def upload_input_macro(env): upload_template = env.get_template("components/upload_input.html") return getattr(upload_template.module, "UploadInput") + @pytest.fixture def checkbox_input_macro(env): checkbox_template = env.get_template("components/checkbox_input.html") return getattr(checkbox_template.module, "CheckboxInput") + @pytest.fixture def initial_value_form(scope="function"): return InitialValueForm() + def write_template(content, name): with open("js/test_templates/{}".format(name), "w") as fh: fh.write(content) + def test_make_checkbox_input_template(checkbox_input_macro, initial_value_form): initial_value_form.datafield.widget = CheckboxInput() rendered_checkbox_macro = checkbox_input_macro(initial_value_form.datafield) write_template(rendered_checkbox_macro, "checkbox_input_template.html") + def test_make_upload_input_template(upload_input_macro, initial_value_form): rendered_upload_macro = upload_input_macro(initial_value_form.datafield) write_template(rendered_upload_macro, "upload_input_template.html") + def test_make_upload_input_error_template(upload_input_macro, initial_value_form): initial_value_form.validate() rendered_upload_macro = upload_input_macro(initial_value_form.errorfield)