Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: added tests #17

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 168 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ test('ctx.state.l converts path to locale', async t => {
app.use(ctx => {
ctx.state.locale = 'es';
ctx.body = {
path: ctx.state.l('/random/path')
path: ctx.state.l('/random/path'),
pathBlank: ctx.state.l()
};
});

const res = await request(app.listen()).get('/en');

t.is(res.body.path, '/es/random/path');
t.is(res.body.pathBlank, '/es');
});

test('returns correct locale from path', async t => {
Expand Down Expand Up @@ -144,6 +146,111 @@ test('returns correct locale from Accept-Language header', async t => {
t.is(res.body.locale, 'en');
});

test('returns correct locale from user', async t => {
const app = new Koa();
const i18n = new I18N({ phrases, directory });

app.use(async (ctx, next) => {
ctx.state.user = {};
ctx.state.user.save = () => {};
ctx.state.user.last_locale = 'en';
ctx.isAuthenticated = () => true;
ctx.request.acceptsLanguages = () => false;
await next();
});
app.use(session());
app.use(i18n.middleware);

app.use(ctx => {
const { locale } = ctx;
ctx.body = { locale };
ctx.status = 200;
});

const res = await request(app.listen()).get('/');

t.is(res.status, 200);
t.is(res.body.locale, 'en');
});

test('returns correct locale from default', async t => {
const app = new Koa();
const i18n = new I18N({ phrases, directory });

app.use(async (ctx, next) => {
ctx.request.acceptsLanguages = () => false;
await next();
});
app.use(session());
app.use(i18n.middleware);

app.use(ctx => {
const { locale } = ctx;
ctx.body = { locale };
ctx.status = 200;
});

const res = await request(app.listen()).get('/');

t.is(res.status, 200);
t.is(res.body.locale, 'en');
});

test('redirects if locale is not avaiable', async t => {
const app = new Koa();
const i18n = new I18N({ phrases, directory });

app.use(async (ctx, next) => {
ctx.state.user = {};
ctx.state.user.save = () => {};
ctx.state.user.last_locale = 'de';
ctx.isAuthenticated = () => true;
ctx.request.acceptsLanguages = () => false;
await next();
});
app.use(session());
app.use(i18n.middleware);

app.use(ctx => {
const { locale } = ctx;
ctx.body = { locale };
ctx.status = 200;
});

const res = await request(app.listen()).get('/');

t.is(res.status, 302);
t.is(res.header.location, '/en/');
});

test('redirects if locale is not avaiable with query', async t => {
const app = new Koa();
const i18n = new I18N({ phrases, directory });

app.use(async (ctx, next) => {
ctx.state.user = {};
ctx.state.user.save = () => {};
ctx.state.user.last_locale = 'de';
ctx.isAuthenticated = () => true;
ctx.request.acceptsLanguages = () => false;
ctx.query = { a: 'b' };
await next();
});
app.use(session());
app.use(i18n.middleware);

app.use(ctx => {
const { locale } = ctx;
ctx.body = { locale };
ctx.status = 200;
});

const res = await request(app.listen()).get('/');

t.is(res.status, 302);
t.is(res.header.location, '/en/??a=b');
});

test('prefers path over cookie', async t => {
const app = new Koa();
const i18n = new I18N({ phrases, directory });
Expand Down Expand Up @@ -409,3 +516,63 @@ test('ctx.translates throws error with no_translate', async t => {
t.is(res.status, 500);
t.is(res.body.message, 'Hello there!');
});

test('errors if default locale does not exist', t => {
t.throws(() => new I18N({ phrases, directory, defaultLocale: 'de' }), {
message: 'Default locale of de must be included in list of locales'
});
});

test('translate default locale', t => {
const i18n = new I18N({ phrases, directory });

t.is(i18n.translate('hello'), 'hello');
});

test('does not redirect if method is not GET', async t => {
const app = new Koa();
const i18n = new I18N({ phrases, directory });

app.use(session());
app.use(i18n.middleware);
app.use(i18n.redirect);

app.use(ctx => {
const { locale } = ctx;
ctx.body = { locale };
ctx.status = 200;
});

const res = await request(app.listen())
.get('/login.html')
.set('Cookie', ['locale=es']);
t.is(res.status, 200);
});

test('redirects sets users last_locale', async t => {
const app = new Koa();
const i18n = new I18N({ phrases, directory });

app.use(async (ctx, next) => {
ctx.state.user = { last_locale: 'en' };
ctx.state.user.save = () => {};
ctx.isAuthenticated = () => true;
await next();
});
app.use(session());
app.use(i18n.middleware);
app.use(i18n.redirect);

app.use(ctx => {
const { locale } = ctx;
ctx.body = {
locale,
last_locale: ctx.state.user.last_locale
};
ctx.status = 200;
});

const res = await request(app.listen()).get('/en/login');
t.is(res.status, 200);
t.is(res.body.last_locale, 'en');
});