Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial get work #152

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

export function Get(inputs) {
function get(name) {
let value = ''
let foundInput = inputs.find(input => input.name === name)
if (foundInput && typeof foundInput.value === 'object') {
let values = []
foundInput.value.forEach(subInput => {
if (subInput.value) {
values.push(subInput.name)
}
})
value = values
} else if (foundInput && foundInput.value !== undefined) {
value = foundInput.value
}
// Return radio buttons as a single value chosen, not a single entry array.
if (foundInput && foundInput.tagName === 'TANGY-RADIO-BUTTONS' && Array.isArray(value)) {
value = (value.length > 0) ? value[0] : ''
}
if (!value) {
value = ''
}
return value
}
return this
}
40 changes: 40 additions & 0 deletions test/tangy-form-response-model_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title>loc test</title>

<script src="../node_modules/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
<script src="../node_modules/wct-browser-legacy/browser.js"></script>

</head>
<body>

<script type="module">
import { Get, Set } from '../helpers.js'
import stub from './stub.js'

suite('helpers', () => {
test('Get should return value', () => {
assert.equal(Get(stub.tangyFormResponse, 'input1'), 'Foo')
})
test('Get should behave as factory then get should return value', () => {
const get = Get(stub.tangyFormResponse)
assert.equal(get('input1'), 'Foo')
})
test('Set should return response with updated value', () => {
const tangyFormResponseModel = Set(stub.tangyFormResponse, 'input1', 'Bar')
assert.equal(tangyFormResponseModel.get('input1'), 'Bar')
})
test('Set should behave as factory then set should return response with updated value', () => {
const set = Set(stub.tangyFormResponse)
const tangyFormResponseModel = set('input1', 'Bar')
assert.equal(tangyFormResponseModel.get('input1'), 'Bar')
})
})
</script>

</body>
</html>