|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const Validator = require("../../lib/validator"); |
| 4 | +const v = new Validator(); |
| 5 | + |
| 6 | +describe("Test rule: currency", () => { |
| 7 | + it("should have decimal optional, and correctly placed if present", () => { |
| 8 | + const check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true}); |
| 9 | + expect(check("$12.2")).toEqual(true); |
| 10 | + expect(check("$12,222.2")).toEqual(true); |
| 11 | + expect(check("$12,222")).toEqual(true); |
| 12 | + expect(check("$12,222.0")).toEqual(true); |
| 13 | + expect(check("$1.22.00")).toEqual([{"actual": "$1.22.00", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]); |
| 14 | + }); |
| 15 | + |
| 16 | + it("should check comma placement is correct", () => { |
| 17 | + const check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true}); |
| 18 | + expect(check("$12.2")).toEqual(true); |
| 19 | + expect(check("$12,222.2")).toEqual(true); |
| 20 | + expect(check("$122,222.2")).toEqual(true); |
| 21 | + expect(check("$1234,222.2")).toEqual([{"actual": "$1234,222.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]); |
| 22 | + expect(check("$1,2,222")).toEqual( [{"actual": "$1,2,222", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("should not allow any currency symbol , if not supplied in schema", () => { |
| 26 | + let check = v.compile({$$root: true, type: "currency"}); |
| 27 | + expect(check("12.2")).toEqual(true); |
| 28 | + expect(check("$12.2")).toEqual( [{"actual": "$12.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should not allow any other currency symbol, other than supplied in schema", () => { |
| 32 | + let check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': false}); |
| 33 | + expect(check("$12.2")).toEqual(true); |
| 34 | + expect(check("#12.2")).toEqual([{"actual": "#12.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}]); |
| 35 | + }); |
| 36 | + |
| 37 | + it("should keep currency symbol optional, if symbolOptional is true in schema", () => { |
| 38 | + let check = v.compile({$$root: true, type: "currency", 'currencySymbol': '$', 'symbolOptional': true}); |
| 39 | + expect(check("$12.2")).toEqual(true); |
| 40 | + expect(check("12.2")).toEqual(true); |
| 41 | + expect(check("#12.2")).toEqual([{"actual": "#12.2", "field": undefined, "message": "The '' must be a valid currency format", "type": "currency"}] |
| 42 | + ); |
| 43 | + }); |
| 44 | +}); |
0 commit comments