0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00
astro/packages/db/test/basics.test.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
3 KiB
JavaScript
Raw Normal View History

2024-01-08 15:31:05 -05:00
import { expect } from 'chai';
import { load as cheerioLoad } from 'cheerio';
2024-01-09 16:33:35 -05:00
import { loadFixture } from '../../astro/test/test-utils.js';
2024-01-12 16:39:30 -05:00
import testAdapter from '../../astro/test/test-adapter.js';
2024-01-08 15:31:05 -05:00
2024-02-07 20:48:43 -08:00
// TODO(fks): Rename this to something more generic/generally useful
// like `ASTRO_MONOREPO_TEST_ENV` if @astrojs/db is merged into astro.
process.env.ASTRO_DB_TEST_ENV = '1';
2024-01-08 15:31:05 -05:00
describe('astro:db', () => {
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/basics/', import.meta.url),
2024-01-12 16:39:30 -05:00
output: 'server',
2024-01-24 16:07:17 -08:00
adapter: testAdapter(),
2024-01-08 15:31:05 -05:00
});
});
2024-01-12 16:39:30 -05:00
describe('production', () => {
2024-01-08 15:31:05 -05:00
before(async () => {
await fixture.build();
});
it('Prints the list of authors', async () => {
2024-01-12 16:39:30 -05:00
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/');
const res = await app.render(request);
const html = await res.text();
2024-01-08 15:31:05 -05:00
const $ = cheerioLoad(html);
const ul = $('.authors-list');
2024-01-09 16:33:35 -05:00
expect(ul.children()).to.have.a.lengthOf(5);
expect(ul.children().eq(0).text()).to.equal('Ben');
2024-01-08 15:31:05 -05:00
});
2024-01-12 16:39:30 -05:00
it('Errors when inserting to a readonly collection', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/insert-into-readonly');
const res = await app.render(request);
const html = await res.text();
const $ = cheerioLoad(html);
expect($('#error').text()).to.equal('The [Author] collection is read-only.');
});
it('Does not error when inserting into writable collection', async () => {
const app = await fixture.loadTestAdapterApp();
const request = new Request('http://example.com/insert-into-writable');
const res = await app.render(request);
const html = await res.text();
const $ = cheerioLoad(html);
expect($('#error').text()).to.equal('');
});
describe('Expression defaults', () => {
let app;
before(async () => {
app = await fixture.loadTestAdapterApp();
});
it('Allows expression defaults for date fields', async () => {
const request = new Request('http://example.com/');
const res = await app.render(request);
const html = await res.text();
const $ = cheerioLoad(html);
const themeAdded = $($('.themes-list .theme-added')[0]).text();
expect(new Date(themeAdded).getTime()).to.not.be.NaN;
});
it('Allows expression defaults for text fields', async () => {
const request = new Request('http://example.com/');
const res = await app.render(request);
const html = await res.text();
const $ = cheerioLoad(html);
const themeOwner = $($('.themes-list .theme-owner')[0]).text();
expect(themeOwner).to.equal('');
});
it('Allows expression defaults for boolean fields', async () => {
const request = new Request('http://example.com/');
const res = await app.render(request);
const html = await res.text();
const $ = cheerioLoad(html);
const themeDark = $($('.themes-list .theme-dark')[0]).text();
expect(themeDark).to.equal('dark mode');
});
})
2024-01-08 15:31:05 -05:00
});
});