Skip to content

Commit

Permalink
Add the ability to generate a random zip code (#964)
Browse files Browse the repository at this point in the history
* Add the ability to generate a random zip code

* Fix linting

* Fix typow

---------

Co-authored-by: Brian Hall <brianhall@Brians-Laptop.local>
  • Loading branch information
brianhall and Brian Hall committed May 10, 2024
1 parent cea085a commit 900c93f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class BrokerProtectionPage {
await expect(this.page.getByLabel('Last Name:')).toHaveValue('Smith')
await expect(this.page.getByLabel('Phone Number:')).toHaveValue(/^\d{10}$/)
await expect(this.page.getByLabel('State:')).toHaveValue('IL')
await expect(this.page.getByLabel('Zip Code:')).toHaveValue(/^\d{5}$/)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
{
"type": "state",
"selector": "#state"
},
{
"type": "$generated_zip_code$",
"selector": "#user_zip_code"
}
]
},
Expand Down
4 changes: 4 additions & 0 deletions integration-test/test-pages/broker-protection/pages/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<option value="IL">Illinois</option>
</select>
</label>
<label>
Zip Code:
<input type="text" name="zip_code" id="user_zip_code">
</label>
<button class="btn-sbmt">Submit</button>
</form>
<script>
Expand Down
7 changes: 7 additions & 0 deletions src/features/broker-protection/actions/fill-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export function fillMany (root, elements, data) {
results.push(setImageUpload(inputElem))
} else if (element.type === '$generated_phone_number$') {
results.push(setValueForInput(inputElem, generatePhoneNumber()))
} else if (element.type === '$generated_zip_code$') {
results.push(setValueForInput(inputElem, generateZipCode()))
} else {
if (!Object.prototype.hasOwnProperty.call(data, element.type)) {
results.push({ result: false, error: `element found with selector '${element.selector}', but data didn't contain the key '${element.type}'` })
Expand Down Expand Up @@ -175,3 +177,8 @@ export function generatePhoneNumber () {

return `${areaCode}${exchangeCode}${lineNumber}`
}

export function generateZipCode () {
const zipCode = generateRandomInt(10000, 99999).toString()
return zipCode
}
11 changes: 10 additions & 1 deletion unit-test/broker-protection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { replaceTemplatedUrl } from '../src/features/broker-protection/actions/b
import { processTemplateStringWithUserData } from '../src/features/broker-protection/actions/build-url-transforms.js'
import { names } from '../src/features/broker-protection/comparisons/constants.js'
import { generateRandomInt } from '../src/features/broker-protection/utils.js'
import { generatePhoneNumber } from '../src/features/broker-protection/actions/fill-form.js'
import { generatePhoneNumber, generateZipCode } from '../src/features/broker-protection/actions/fill-form.js'
import { CityStateExtractor } from '../src/features/broker-protection/extractors/address.js'

describe('Actions', () => {
Expand Down Expand Up @@ -556,6 +556,15 @@ describe('Actions', () => {
expect(phoneNumber).toMatch(/^\d{10}$/)
})
})
describe('generateZipCode', () => {
it('generates a string of integers of an appropriate size', () => {
const zipCode = generateZipCode()

expect(typeof zipCode).toEqual('string')
expect(zipCode.length).toBe(5)
expect(zipCode).toMatch(/^\d{5}$/)
})
})
})
})

Expand Down

0 comments on commit 900c93f

Please sign in to comment.