|
| 1 | +import { MessageFormat } from "./message-format.ts"; |
| 2 | + |
| 3 | +describe("Message Format", () => { |
| 4 | + it("should render for no variable substitution", () => { |
| 5 | + const formatted = MessageFormat("Hello world!", "en").format(); |
| 6 | + |
| 7 | + expect(formatted).toEqual("Hello world!"); |
| 8 | + }); |
| 9 | + |
| 10 | + describe("Variable chunk", () => { |
| 11 | + it("should render basic variable substitution", () => { |
| 12 | + const formatted = MessageFormat("Hello {somewhere}!", "en").format({ |
| 13 | + somewhere: "world", |
| 14 | + }); |
| 15 | + |
| 16 | + expect(formatted).toEqual("Hello world!"); |
| 17 | + }); |
| 18 | + |
| 19 | + it("should render multiple variable substitution", () => { |
| 20 | + const formatted = MessageFormat("{0} {1}{2}", "en").format({ |
| 21 | + 0: "Hello", |
| 22 | + 1: "world", |
| 23 | + 2: "!", |
| 24 | + }); |
| 25 | + |
| 26 | + expect(formatted).toEqual("Hello world!"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should apply locale formatting to numbers", () => { |
| 30 | + const formatted = MessageFormat("{0}", "de-DE").format({ |
| 31 | + 0: 123123.123123, |
| 32 | + }); |
| 33 | + |
| 34 | + // java equivalent |
| 35 | + // new MessageFormat("{0}", Locale.GERMAN).format(new Object[]{123123.123123}) |
| 36 | + expect(formatted).toEqual("123.123,123"); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should render for a complex pattern", () => { |
| 41 | + const formatted = MessageFormat( |
| 42 | + "{0} {1,choice,0#world|1#team|1<moto}{2} {3}", |
| 43 | + "en", |
| 44 | + ).format({ 0: "Hello", 1: 0, 2: "!", 3: "🚀" }); |
| 45 | + |
| 46 | + expect(formatted).toEqual("Hello world! 🚀"); |
| 47 | + }); |
| 48 | + |
| 49 | + describe("Choice subformat", () => { |
| 50 | + const messageFormat = MessageFormat( |
| 51 | + "Hello {0,choice,0#world|1#team|1<moto}!", |
| 52 | + "en", |
| 53 | + ); |
| 54 | + |
| 55 | + it("should choose appropriate choice", () => { |
| 56 | + expect(messageFormat.format({ 0: 0 })).toEqual("Hello world!"); |
| 57 | + expect(messageFormat.format({ 0: 1 })).toEqual("Hello team!"); |
| 58 | + expect(messageFormat.format({ 0: 2 })).toEqual("Hello moto!"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("should use the first for no match found", () => { |
| 62 | + expect(messageFormat.format({ 0: -1 })).toEqual("Hello world!"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should handle floating point numbers", () => { |
| 66 | + expect(messageFormat.format({ 0: 1.5 })).toEqual("Hello moto!"); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should handle invalid input", () => { |
| 70 | + expect(() => messageFormat.format({ 0: "ahhh" })).toThrow( |
| 71 | + "Argument 0 'ahhh' needs to be a number", |
| 72 | + ); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should handle empty arg", () => { |
| 76 | + expect(() => messageFormat.format({ 0: "" })).toThrow( |
| 77 | + "Argument 0 '' needs to be a number", |
| 78 | + ); |
| 79 | + }); |
| 80 | + }); |
| 81 | +}); |
0 commit comments