Skip to content

Commit

Permalink
test tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
cathysarisky committed Oct 29, 2024
1 parent ba30684 commit 156775b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
4 changes: 3 additions & 1 deletion ghost/email-service/lib/EmailRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ function escapeHtml(unsafe) {
}

function formatDateLong(date, timezone, locale = 'en-gb') {
if (locale === 'en') { locale = 'en-gb'; }
if (locale === 'en') {
locale = 'en-gb';
}
return DateTime.fromJSDate(date).setZone(timezone).setLocale(locale).toLocaleString({
year: 'numeric',
month: 'long',
Expand Down
80 changes: 79 additions & 1 deletion ghost/email-service/test/email-renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,15 @@ const getMembersValidationKey = () => {
// load the i18n module
const i18nLib = require('@tryghost/i18n');
const i18n = i18nLib('en', 'newsletter');

const t = (key, options) => {
return i18n.t(key, options);
};

const i18nFr = i18nLib('fr', 'newsletter');
const tFr = (key, options) => {
return i18nFr.t(key, options);
};

describe('Email renderer', function () {
let logStub;

Expand Down Expand Up @@ -350,6 +354,80 @@ describe('Email renderer', function () {
});
});

describe('buildReplacementDefinitions with locales', function () {
let emailRenderer;
let newsletter;
let member;
let labsEnabled = true;
beforeEach(function () {
labsEnabled = false;
emailRenderer = new EmailRenderer({
urlUtils: {
urlFor: () => 'http://example.com/subdirectory/'
},
labs: {
isSet: () => labsEnabled
},
settingsCache: {
get: (key) => {
if (key === 'timezone') {
return 'UTC';
}
}
},
settingsHelpers: {getMembersValidationKey,createUnsubscribeUrl},
t: t
});
newsletter = createModel({
uuid: 'newsletteruuid'
});
member = {
id: '456',
uuid: 'myuuid',
name: 'Test User',
email: 'test@example.com',
createdAt: new Date(2023, 2, 13, 12, 0),
status: 'free'
};
});

it('handles dates when the locale is en', function () {
const html = '%%{created_at}%%';
const replacements = emailRenderer.buildReplacementDefinitions({html, newsletterUuid: newsletter.get('uuid')});
assert.equal(replacements.length, 2);
assert.equal(replacements[0].token.toString(), '/%%\\{created_at\\}%%/g');
assert.equal(replacements[0].id, 'created_at');
assert.equal(replacements[0].getValue(member), '13 March 2023');
});
it('handles dates when the locale is fr', function () {
emailRenderer = new EmailRenderer({
urlUtils: {
urlFor: () => 'http://example.com/subdirectory/'
},
labs: {
isSet: () => labsEnabled
},
settingsCache: {
get: (key) => {
if (key === 'timezone') {
return 'UTC';
}
if (key === 'locale') {
return 'fr';
}
}
},
settingsHelpers: {getMembersValidationKey,createUnsubscribeUrl},
t: tFr
});
const html = '%%{created_at}%%';
const replacements = emailRenderer.buildReplacementDefinitions({html, newsletterUuid: newsletter.get('uuid')});
assert.equal(replacements.length, 2);
assert.equal(replacements[0].token.toString(), '/%%\\{created_at\\}%%/g');
assert.equal(replacements[0].id, 'created_at');
assert.equal(replacements[0].getValue(member), '13 mars 2023');
});
});
describe('isMemberTrialing', function () {
let emailRenderer;

Expand Down
2 changes: 1 addition & 1 deletion ghost/i18n/test/i18n.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('i18n', function () {
});
});
describe('newsletter i18n', function () {
it('should have a prefix and suffix for the newsletter interpolation', async function () {
it('should be able to translate and interpolate a date', async function () {
const t = i18n('fr', 'newsletter').t;
assert.equal(t('Your subscription will renew on {date}.', {date: '8 Oct 2024'}), 'Votre abonnement sera renouvelé le 8 Oct 2024.');
});
Expand Down

0 comments on commit 156775b

Please sign in to comment.