Skip to content

Commit

Permalink
Only two decimals
Browse files Browse the repository at this point in the history
Cypress tests
  • Loading branch information
lotorvik committed May 30, 2024
1 parent 92fcf4d commit d0ccd47
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 9 deletions.
81 changes: 80 additions & 1 deletion mocks/mocks/data/formio-api/number.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"description": "",
"additionalDescriptionText": "",
"validate": {
"required": true,
"required": false,
"custom": "",
"min": 10,
"max": 100,
Expand All @@ -62,6 +62,7 @@
"tableView": false,
"validateOn": "blur",
"type": "number",
"inputType": "numeric",
"spellcheck": false,
"navId": "eqzaqlm",
"placeholder": "",
Expand Down Expand Up @@ -109,6 +110,84 @@
"allowMultipleMasks": false,
"addons": [],
"id": "eulnrki"
},
{
"fieldSize": "input--m",
"description": "",
"additionalDescriptionText": "",
"validate": {
"required": false,
"custom": "",
"min": "",
"max": "",
"customPrivate": false,
"strictDateValidation": false,
"multiple": false,
"unique": false,
"step": "any",
"integer": ""
},
"errors": "",
"conditional": {
"show": null,
"when": null,
"eq": ""
},
"customConditional": "",
"label": "Desimaltall",
"additionalDescriptionLabel": "",
"key": "number2",
"input": true,
"tableView": false,
"validateOn": "blur",
"type": "number",
"spellcheck": false,
"navId": "eqzaqsm",
"placeholder": "",
"prefix": "",
"customClass": "",
"suffix": "",
"multiple": false,
"defaultValue": null,
"protected": false,
"unique": false,
"persistent": true,
"hidden": false,
"clearOnHide": true,
"refreshOn": "",
"redrawOn": "",
"modalEdit": false,
"dataGridLabel": false,
"labelPosition": "top",
"errorLabel": "",
"tooltip": "",
"hideLabel": false,
"tabindex": "",
"disabled": false,
"autofocus": false,
"dbIndex": false,
"customDefaultValue": "",
"calculateValue": "",
"calculateServer": false,
"widget": {
"type": "input"
},
"attributes": {},
"overlay": {
"style": "",
"left": "",
"top": "",
"width": "",
"height": ""
},
"allowCalculateOverride": false,
"encrypted": false,
"showCharCount": false,
"showWordCount": false,
"properties": {},
"allowMultipleMasks": false,
"addons": [],
"id": "eqzaqsm"
}
],
"placeholder": "",
Expand Down
49 changes: 45 additions & 4 deletions packages/fyllut/cypress/e2e/components/number.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Tests that the number component is working as expected
*/

import { TEXTS } from '@navikt/skjemadigitalisering-shared-domain';

describe('number component', () => {
beforeEach(() => {
cy.defaultIntercepts();
Expand All @@ -10,15 +12,54 @@ describe('number component', () => {
cy.clickStart();
});

describe('happy path', () => {
it('should render number component', () => {
cy.findByRole('textbox', { name: 'Tall' }).should('exist').type('50');
describe('decimals', () => {
it('Try and set decimal values', () => {
cy.findByRole('textbox', { name: /^Tall/ }).should('exist').type('10,1');
cy.findByRole('textbox', { name: /^Desimaltall/ })
.should('exist')
.type('10,1');
cy.clickNextStep();
cy.findByRole('link', { name: `Tall er ikke et gyldig heltall.` }).should('exist');
cy.findByRole('textbox', { name: /^Tall/ }).should('exist').type('{selectall}10');
cy.clickNextStep();
cy.findByRole('heading', { name: 'Oppsummering' }).should('exist');
});

it('Set decimal value with dot, change to comma when going back from summary', () => {
cy.findByRole('textbox', { name: /^Desimaltall/ })
.should('exist')
.type('10.1');
cy.clickNextStep();
cy.findByRoleWhenAttached('link', { name: TEXTS.grensesnitt.summaryPage.editAnswers }).should('exist').click();
cy.findByRole('textbox', {
name: /^Desimaltall/,
}).should('have.value', '10,1');
});

it('Set decimal value with spaces, change to no spaces when going back from summary', () => {
cy.findByRole('textbox', { name: /^Desimaltall/ })
.should('exist')
.type('1 0 0 0 0 0,1');
cy.clickNextStep();
cy.findByRoleWhenAttached('link', { name: TEXTS.grensesnitt.summaryPage.editAnswers }).should('exist').click();
cy.findByRole('textbox', {
name: /^Desimaltall/,
}).should('have.value', '100\u00a0000,1');
});
});

describe('integer', () => {
it('Try and set integer values', () => {
cy.findByRole('textbox', { name: /^Tall/ }).should('exist').type('10');
cy.findByRole('textbox', { name: /^Desimaltall/ })
.should('exist')
.type('10');
cy.clickNextStep();
cy.findByRole('heading', { name: 'Oppsummering' }).should('exist');
});
});

describe('errors', () => {
describe('min/max values', () => {
// Min = 10
it('should show error for min value', () => {
cy.findByRole('textbox', { name: 'Tall' }).should('exist').type('9');
Expand Down
4 changes: 2 additions & 2 deletions packages/shared-domain/src/texts/validering.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ export const validering = {
// Number
max: '{{field}} kan ikke være større enn {{max}}.',
min: '{{field}} kan ikke være mindre enn {{min}}.',
integer: '{{field}} er ikke et gyldig heltall.',
decimal: '{{field}} er ikke et gyldig desimaltall.',
integer: '{{field}} er ikke et gyldig tall.',
decimal: '{{field}} er ikke et gyldig desimaltall, med maksimalt to desimaler.',
};
4 changes: 3 additions & 1 deletion packages/shared-domain/src/utils/numberUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ describe('numberValidators', () => {
expect(numberUtils.isValidDecimal('-0.5')).toBe(true);
expect(numberUtils.isValidDecimal('-0.1')).toBe(true);
expect(numberUtils.isValidDecimal('0')).toBe(true);
expect(numberUtils.isValidDecimal('1')).toBe(true);
expect(numberUtils.isValidDecimal('1.5')).toBe(true);
expect(numberUtils.isValidDecimal('1.123456789')).toBe(true);
expect(numberUtils.isValidDecimal('1.12')).toBe(true);
expect(numberUtils.isValidDecimal('123456789.0')).toBe(true);
});

Expand All @@ -16,6 +17,7 @@ describe('numberValidators', () => {
expect(numberUtils.isValidDecimal('1.1a')).toBe(false);
expect(numberUtils.isValidDecimal('1.1a')).toBe(false);
expect(numberUtils.isValidDecimal('1.1.1')).toBe(false);
expect(numberUtils.isValidDecimal('1.123')).toBe(false);
// Comma is valid in NO, but we need to convert comma to dot before validating
expect(numberUtils.isValidDecimal('1,1')).toBe(false);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-domain/src/utils/numberUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const isValidDecimal = (value: string) => {
return /^-?\d+\.?\d*$/.test(value);
return /^-?\d+\.?\d{0,2}$/.test(value);
};

const isValidInteger = (value: string) => {
Expand Down

0 comments on commit d0ccd47

Please sign in to comment.