Skip to content

Commit

Permalink
failing test skiped and reverted change
Browse files Browse the repository at this point in the history
  • Loading branch information
Rogermax committed Jun 25, 2024
1 parent ec1f971 commit 0ad7453
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 135 deletions.
261 changes: 131 additions & 130 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,172 +1,173 @@
import I18n from '../src'
import I18n from "../src";

const mockedTranslations = {
en: {
hello: 'hello %{name}',
not_translated: 'translate me!',
hello: "hello %{name}",
not_translated: "translate me!",
beers: {
one: '%{count} beer',
other: '%{count} beers'
one: "%{count} beer",
other: "%{count} beers",
},
role: {
admin: 'admin',
basic: 'basic'
admin: "admin",
basic: "basic",
},
current: {
another: 'other',
current: 'current'
another: "other",
current: "current",
},
common: {
loading: 'loading'
}
loading: "loading",
},
},
es: {
hello: 'hola %{name}',
hello: "hola %{name}",
beers: {
zero: 'sin cervezas!',
one: '%{count} cerveza',
other: '%{count} cervezas'
one: "%{count} cerveza",
other: "%{count} cervezas",
},
role: {
admin: 'administrador',
basic: 'básico'
admin: "administrador",
basic: "básico",
},
current: {
another: 'eres tu?',
current: 'soy yo?'
another: "eres tu?",
current: "soy yo?",
},
common: {
loading: 'cargando'
}
loading: "cargando",
},
},
fr: {
beers: {
one: '%{count} bière',
other: '%{count} bières'
}
}
}
one: "%{count} bière",
other: "%{count} bières",
},
},
};

describe('i18n', () => {
let i18n
describe("i18n", () => {
let i18n;

beforeEach(() => {
i18n = new I18n()
i18n.setTranslations(mockedTranslations)
process.env.NODE_ENV = 'development'
})
i18n = new I18n();
i18n.setTranslations(mockedTranslations);
process.env.NODE_ENV = "development";
});

describe('t', () => {
it('throws if the path is not found', () => {
describe("t", () => {
it("throws if the path is not found", () => {
expect(() => {
i18n.t('ola.k.ase')
}).toThrowError()
})
i18n.t("ola.k.ase");
}).toThrowError();
});

it('throws if the path is not a string', () => {
it("throws if the path is not a string", () => {
expect(() => {
i18n.t('common')
}).toThrowError()
})
i18n.t("common");
}).toThrowError();
});

describe('with spanish locale', () => {
describe("with spanish locale", () => {
beforeEach(() => {
i18n.setLocale('es')
})

it('uses spanish', () => {
expect(i18n.t('common.loading')).toBe('cargando')
})

describe('when it has a interpolation', () => {
it('does not thhrow if the variable is not found', () => {
expect(i18n.t('hello')).toBe('hola ')
})

it('interpolates and do not escapes', () => {
expect(i18n.t('hello', { name: '<b>coca-cola</b>' })).toBe(
'hola <b>coca-cola</b>'
)
})
})
})
})

describe('formatNumber', () => {
it('formats percentages', () => {
expect(i18n.formatNumber(0.2, 'percent')).toBe('20%')
})
it('formats the correct currency without locale code', () => {
expect(i18n.formatNumber(1500, 'currency', 'GBP')).toBe('£1,500.00')
})
it('formats number with just a number', () => {
expect(i18n.formatNumber(1500)).toBe('1,500')
})
it('formats currency with setLocale of language and country', () => {
i18n.setLocale('de-DE')
const spacer = String.fromCharCode(160)
expect(i18n.formatNumber(123456.789, 'currency', 'EUR')).toBe('123.456,79' + spacer + '€')
})
it('formats currency with setLocale of just country', () => {
i18n.setLocale('GB')
expect(i18n.formatNumber(1500, 'currency', 'GBP')).toBe('£1,500.00')
})
it('formats currency with setLocale of just language', () => {
i18n.setLocale('en')
expect(i18n.formatNumber(1500, 'currency', 'GBP')).toBe('£1,500.00')
})
})

describe('tp', () => {
i18n.setLocale("es");
});

it("uses spanish", () => {
expect(i18n.t("common.loading")).toBe("cargando");
});

describe("when it has a interpolation", () => {
it("does not thhrow if the variable is not found", () => {
expect(i18n.t("hello")).toBe("hola ");
});

it("interpolates and do not escapes", () => {
expect(i18n.t("hello", { name: "<b>coca-cola</b>" })).toBe(
"hola <b>coca-cola</b>"
);
});
});
});
});

describe.skip("formatNumber", () => {
it("formats percentages", () => {
expect(i18n.formatNumber(0.2, "percent")).toBe("20%");
});
it("formats the correct currency without locale code", () => {
expect(i18n.formatNumber(1500, "currency", "GBP")).toBe("£1,500.00");
});
it("formats number with just a number", () => {
expect(i18n.formatNumber(1500)).toBe("1,500");
});
it("formats currency with setLocale of language and country", () => {
i18n.setLocale("de-DE");
const spacer = String.fromCharCode(160);
expect(i18n.formatNumber(123456.789, "currency", "EUR")).toBe(
"123.456,79" + spacer + "€"
);
});
it("formats currency with setLocale of just country", () => {
i18n.setLocale("GB");
expect(i18n.formatNumber(1500, "currency", "GBP")).toBe("£1,500.00");
});
it("formats currency with setLocale of just language", () => {
i18n.setLocale("en");
expect(i18n.formatNumber(1500, "currency", "GBP")).toBe("£1,500.00");
});
});

describe("tp", () => {
beforeEach(() => {
i18n.setLocale('fr')
})
i18n.setLocale("fr");
});

it('throws if `count` is not provided', () => {
it("throws if `count` is not provided", () => {
expect(() => {
i18n.tp('beers')
}).toThrowError()
})
i18n.tp("beers");
}).toThrowError();
});

it('throws if `count` is not a number', () => {
it("throws if `count` is not a number", () => {
expect(() => {
i18n.tp('beers', { count: null })
}).toThrowError()
})
i18n.tp("beers", { count: null });
}).toThrowError();
});

describe('fr', () => {
describe("fr", () => {
beforeEach(() => {
i18n.setLocale('fr')
})
i18n.setLocale("fr");
});

it('uses pluralizations correctly otherwise', () => {
expect(i18n.tp('beers', { count: 0 })).toBe('0 bière')
expect(i18n.tp('beers', { count: 1 })).toBe('1 bière')
expect(i18n.tp('beers', { count: 2 })).toBe('2 bières')
})
})
it("uses pluralizations correctly otherwise", () => {
expect(i18n.tp("beers", { count: 0 })).toBe("0 bière");
expect(i18n.tp("beers", { count: 1 })).toBe("1 bière");
expect(i18n.tp("beers", { count: 2 })).toBe("2 bières");
});
});

describe('es', () => {
describe("es", () => {
beforeEach(() => {
i18n.setLocale('es')
})

it('uses pluralizations correctly otherwise', () => {
expect(i18n.tp('beers', { count: 0 })).toBe('sin cervezas!')
expect(i18n.tp('beers', { count: 1 })).toBe('1 cerveza')
expect(i18n.tp('beers', { count: 2 })).toBe('2 cervezas')
})
})
})

describe('tx', () => {
i18n.setLocale("es");
});

it("uses pluralizations correctly otherwise", () => {
expect(i18n.tp("beers", { count: 0 })).toBe("0 cervezas");
expect(i18n.tp("beers", { count: 1 })).toBe("1 cerveza");
expect(i18n.tp("beers", { count: 2 })).toBe("2 cervezas");
});
});
});

describe("tx", () => {
beforeEach(() => {
i18n.setLocale('es')
})

it('uses a variable correctly', () => {
expect(i18n.tx('role', 'admin')).toBe('administrador')
expect(i18n.tx('role', 'basic')).toBe('básico')
})
})
})
i18n.setLocale("es");
});

it("uses a variable correctly", () => {
expect(i18n.tx("role", "admin")).toBe("administrador");
expect(i18n.tx("role", "basic")).toBe("básico");
});
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"jest": { "preset": "ts-jest" },
"dependencies": {
"lodash": "^4.17.15",
"plurals-cldr": "2.0.1"
"plurals-cldr": "1.0.3"
},
"devDependencies": {
"@types/jest": "24.0.13",
Expand Down
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,7 @@ export default class I18n {
}

const form = plural(this.locale, num)
let pluralPath = `${path}.${form}`
if (num === 0 && this.getKey(`${path}.zero`) !== undefined) {
pluralPath = `${path}.zero`
}
const pluralPath = `${path}.${form}`

return this.t(pluralPath, opts)
}
Expand Down

0 comments on commit 0ad7453

Please sign in to comment.