Skip to content

Commit

Permalink
refactor findVarUsage to CanJS with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemitchel committed Nov 14, 2024
1 parent 9a50089 commit 71f163d
Show file tree
Hide file tree
Showing 9 changed files with 507 additions and 257 deletions.
87 changes: 0 additions & 87 deletions legacy/A2J_Tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,93 +1163,6 @@ function updateTOCOnePage () { // TODO - just update only this page's TOC and Ma
updateTOC()
}

function vcGatherUsage (varName) { // 2015-03-27 Search for variable or constant
var html = ''
var count = 0
var pageName
var lowerCaseVarName = varName.toLowerCase()
var regexString = `\\(\\s*${lowerCaseVarName}\\s*\\)|\\%\\s*${lowerCaseVarName}\\s*\\%|\\[\\s*${lowerCaseVarName}\\s*\\]`
var macroRegex = new RegExp(regexString, 'i')
for (pageName in window.gGuide.pages) { // Search text, buttons, help, fields and logic for variable name.
/** @type TPage */
var where = [] // list where it's on this page
var page = window.gGuide.pages[pageName]

const findMatches = (searchTarget, usageItem) => {
// skip check if not string value to check
const prop = usageItem['key']
if (!searchTarget[prop]) { return }
const testValue = searchTarget[prop].toLowerCase()

if (usageItem.type === 'regex') { // check for macro matches, `%%someVar%%`
const matches = testValue.match(macroRegex)
if (matches && matches.length) {
where.push(usageItem.display)
}
} else if (usageItem.type === 'logic') {
if (testValue.indexOf(lowerCaseVarName) !== -1) { // check for logic usage (no macro syntax, `set someVar to "foo"`)
where.push(usageItem.display)
}
} else {
if (testValue === lowerCaseVarName) { // check for varName itself, `someVar`
where.push(usageItem.display)
}
}
}

// check top level page properties
const pageProps = [
{ key: 'name', type: 'regex', display: 'Page Name' },
{ key: 'text', type: 'regex', display: 'Question Text' },
{ key: 'repeatVar', type: 'string', display: 'Counting Variable' },
{ key: 'outerLoopVar', type: 'string', display: 'Outer Loop Variable' },
{ key: 'learn', type: 'regex', display: 'LearnMore Prompt' },
{ key: 'help', type: 'regex', display: 'LearnMore Response' },
{ key: 'helpReader', type: 'regex', display: 'Video Transcript' },
{ key: 'codeBefore', type: 'logic', display: 'Before Logic' },
{ key: 'codeAfter', type: 'logic', display: 'After Logic' }
]
for (const entry of pageProps) {
findMatches(page, entry)
}

// check all page fields
const fieldProps = [
{ key: 'label', type: 'regex', display: 'Field Label' },
{ key: 'name', type: 'string', display: 'Field Variable' },
{ key: 'value', type: 'regex', display: 'Field Default Value' },
{ key: 'invalidPrompt', type: 'regex', display: 'Field Custom Invalid Prompt' },
{ key: 'sample', type: 'regex', display: 'Field Sample Value' }
]
for (const field of page.fields) {
for (const entry of fieldProps) {
findMatches(field, entry)
}
}

// check all buttons
const buttonProps = [
{ key: 'label', type: 'regex', display: 'Button Label' },
{ key: 'name', type: 'string', display: 'Button Variable Name' },
{ key: 'value', type: 'regex', display: 'Button Default Value' },
{ key: 'repeatVar', type: 'string', display: 'Button Counting Variable' },
{ key: 'url', type: 'regex', display: 'Button URL' }
]
for (const button of page.buttons) {
for (const entry of buttonProps) {
findMatches(button, entry)
}
}

if (where.length > 0) { // If we found anything, we'll list the page and its location.
count++
html += ('<li>' + page.name + '</li><ul>' + '<li>' + where.join('<li>') + '</ul>')
}
}

return 'Used in ' + count + ' pages' + '<ul>' + html + '</ul>'
}

// 2014-08-01 Get/restore selected text when setting hyperlink or popup.
// Chrome forgest selection when using Popup picker.
// http://stackoverflow.com/questions/5605401/insert-link-in-contenteditable-element
Expand Down
69 changes: 0 additions & 69 deletions legacy/a2j-tabs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,73 +16,4 @@ describe('legacy/A2J_Tabs', function () {
afterEach(() => { // cleanup globals
window.gGuide = null
})

describe('vcGatherUsage', function () {
it('gathers usage on Page, Field, and Button level property values using variables', function () {
const usageTestPage = new window.TPage()
usageTestPage.fields = [new window.TField()]
usageTestPage.buttons = [new window.TButton()]

window.gGuide.pages = { 'usageTestPage': usageTestPage }

const testProps = {
page: [
{key: 'name', type: 'regex', display: 'Page Name'},
{key: 'text', type: 'regex', display: 'Question Text'},
{key: 'repeatVar', type: 'string', display: 'Counting Variable'},
{key: 'outerLoopVar', type: 'string', display: 'Outer Loop Variable'},
{key: 'learn', type: 'regex', display: 'LearnMore Prompt'},
{key: 'help', type: 'regex', display: 'LearnMore Response'},
{key: 'helpReader', type: 'regex', display: 'Video Transcript'},
{key: 'codeBefore', type: 'logic', display: 'Before Logic'},
{key: 'codeAfter', type: 'logic', display: 'After Logic'}
],
fields: [
{key: 'label', type: 'regex', display: 'Field Label'},
{key: 'name', type: 'string', display: 'Field Variable'},
{key: 'value', type: 'regex', display: 'Field Default Value'},
{key: 'invalidPrompt', type: 'regex', display: 'Field Custom Invalid Prompt'},
{key: 'sample', type: 'regex', display: 'Field Sample Value'}
],
buttons: [
{key: 'label', type: 'regex', display: 'Button Label'},
{key: 'name', type: 'string', display: 'Button Variable Name'},
{key: 'value', type: 'regex', display: 'Button Default Value'},
{key: 'repeatVar', type: 'string', display: 'Button Counting Variable'},
{key: 'url', type: 'regex', display: 'Button URL'}
]
}

const setTestProps = (targetMap, propsToSet) => {
for (const entry of propsToSet) {
const prop = entry.key
if (entry.type === 'regex') {
targetMap[prop] = 'macro style %%[Number NU]%%'
} else if (entry.type === 'logic') {
targetMap[prop] = 'SET [Number NU] TO 1'
} else { // direct var set
targetMap[prop] = 'Number NU'
}
}
}
setTestProps(usageTestPage, testProps.page)
setTestProps(usageTestPage.fields[0], testProps.fields)
setTestProps(usageTestPage.buttons[0], testProps.buttons)

const foundMessage = window.vcGatherUsage('Number NU')
// if found, `display value` will be in foundMessage for each entry
for (const entry of testProps.page) {
const usedInPage = foundMessage.indexOf(entry.display) !== -1
assert.isTrue(usedInPage, `should find Number NU usage in page.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
}
for (const entry of testProps.fields) {
const usedInField = foundMessage.indexOf(entry.display) !== -1
assert.isTrue(usedInField, `should find Number NU usage in field.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
}
for (const entry of testProps.buttons) {
const usedInButton = foundMessage.indexOf(entry.display) !== -1
assert.isTrue(usedInButton, `should find Number NU usage in button.${entry.key} by displaying ${entry.display} in returned foundMessage html`)
}
})
})
})
16 changes: 16 additions & 0 deletions src/variables/editor/editor-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html>

<title>Author Editor Tests</title>

<div id="mocha"></div>
<div id="test-area"></div>

<script type="text/javascript">
window.less = {async: true, fileSync: true};
</script>

<script src="../../node_modules/steal/steal.js"
mocha="bdd"
main="a2jauthor/src/variables/editor/editor-test">
</script>
28 changes: 28 additions & 0 deletions src/variables/editor/editor-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { assert } from 'chai'
import { VariableEditorVM } from './editor'
import { findVarUsage } from './findVarUsage'
import { testPages, expectedHtml } from './testPages'

import 'steal-mocha'

describe('<editor>', () => {
describe('viewModel', () => {
let vm

beforeEach(() => {
vm = new VariableEditorVM()
})
it('smoketest', () => {
assert.equal(vm.initialVariable, undefined, 'should start with no initial variables')
})
})

describe('findVarUsage', () => {
it('gathers usage on Page, Field, and Button level property values using variables', function () {
const { pageCount, html } = findVarUsage('Foo', testPages)

assert.equal(pageCount, 3, 'should return pageCount for variable usage across pages, fields, macros, buttons, and logic')
assert.equal(html, expectedHtml, 'should return html message for found locations')
})
})
})
Loading

0 comments on commit 71f163d

Please sign in to comment.