Skip to content

Commit

Permalink
Break apart tests into smaller files
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed Sep 30, 2023
1 parent 07c9947 commit 1536526
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 157 deletions.
18 changes: 18 additions & 0 deletions apps/tests/src/canvas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';

const test = suite('Canvas');

const ctx = Switch.screen.getContext('2d');

test('`CanvasContext2D#getImageData()`', () => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 1, 1);
const data = ctx.getImageData(0, 0, 1, 1);
assert.equal(data.data[0], 255);
assert.equal(data.data[1], 0);
assert.equal(data.data[2], 0);
assert.equal(data.data[3], 255);
});

test.run();
18 changes: 18 additions & 0 deletions apps/tests/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';

const test = suite('fetch');

test('Request - no `body` for `GET`', () => {
let err: Error | undefined;
try {
new Request('http://example.com', { body: 'bad' });
} catch (_err: any) {
err = _err;
}
assert.ok(err);
assert.equal(err.name, 'TypeError');
assert.equal(err.message, 'Body not allowed for GET or HEAD requests');
});

test.run();
30 changes: 30 additions & 0 deletions apps/tests/src/form-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';

const test = suite('FormData');

test('FormData', async () => {
const file = new File(['conte', new Blob(['nts'])], 'file.txt', {
type: 'text/plain',
});
assert.equal(file.name, 'file.txt');
assert.equal(file.type, 'text/plain');

const form = new FormData();
form.append('file', file);
form.append('string', 'string-value');

const r = new Response(form);
const form2 = await r.formData();
assert.ok(form2 instanceof FormData);
assert.ok(form2.get('string') === 'string-value');
assert.ok(form2.get('missing') === null);

const file2 = form2.get('file');
assert.ok(file2 instanceof File);
assert.ok(file2.name === 'file.txt');
assert.ok(file2.type === 'text/plain');
assert.ok((await file2.text()) === 'contents');
});

test.run();
159 changes: 4 additions & 155 deletions apps/tests/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,155 +1,4 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';

const ctx = Switch.screen.getContext('2d');

test('`Switch.entrypoint` is a string', () => {
assert.type(Switch.entrypoint, 'string');
assert.ok(Switch.entrypoint.length > 0);
});

test('`Switch.cwd()` is a URL string representation', async () => {
const cwd = Switch.cwd();
assert.type(cwd, 'string');
assert.ok(cwd.startsWith('sdmc:/'));
assert.ok(cwd.endsWith('/'));
});

test('`Switch.readDirSync()` works on relative path', () => {
const files = Switch.readDirSync('.');
assert.ok(Array.isArray(files));
assert.ok(files.length > 0);
});

test('`Switch.readDirSync()` works on `sdmc:/` path', () => {
const files = Switch.readDirSync('sdmc:/');
assert.ok(Array.isArray(files));
assert.ok(files.length > 0);
});

test('`Switch.readDirSync()` works on `romfs:/` path', () => {
const files = Switch.readDirSync('romfs:/');
assert.ok(Array.isArray(files));
assert.ok(files.length > 0);
});

test('`Switch.readDirSync()` throws error when directory does not exist', () => {
let err: Error | undefined;
try {
Switch.readDirSync('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.resolveDns()` works', async () => {
const result = await Switch.resolveDns('n8.io');
assert.ok(Array.isArray(result));
assert.ok(result.length > 0);
});

test('`Switch.resolveDns()` rejects with invalid hostname', async () => {
let err: Error | undefined;
try {
await Switch.resolveDns('example-that-definitely-does-not-exist.nxjs');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.readFile()` works with string path', async () => {
const data = await Switch.readFile('romfs:/runtime.js');
assert.ok(data instanceof ArrayBuffer);
assert.ok(data.byteLength > 0);
});

test('`Switch.readFile()` works with URL path', async () => {
const data = await Switch.readFile(new URL(Switch.entrypoint));
assert.ok(data instanceof ArrayBuffer);
assert.ok(data.byteLength > 0);
});

test('`Switch.readFile()` rejects when file does not exist', async () => {
let err: Error | undefined;
try {
await Switch.readFile('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.readFile()` rejects when attempting to read a directory', async () => {
let err: Error | undefined;
try {
await Switch.readFile('.');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.stat()` returns file information', async () => {
const stat = await Switch.stat(Switch.entrypoint);
assert.ok(stat.size > 0);
});

test('`Switch.stat()` rejects when file does not exist', async () => {
let err: Error | undefined;
try {
await Switch.stat('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`CanvasContext2D#getImageData()`', () => {
ctx.fillStyle = 'red';
ctx.fillRect(0, 0, 1, 1);
const data = ctx.getImageData(0, 0, 1, 1);
assert.equal(data.data[0], 255);
assert.equal(data.data[1], 0);
assert.equal(data.data[2], 0);
assert.equal(data.data[3], 255);
});

test('FormData', async () => {
const file = new File(['conte', new Blob(['nts'])], 'file.txt', {
type: 'text/plain',
});
assert.equal(file.name, 'file.txt');
assert.equal(file.type, 'text/plain');

const form = new FormData();
form.append('file', file);
form.append('string', 'string-value');

const r = new Response(form);
const form2 = await r.formData();
assert.ok(form2 instanceof FormData);
assert.ok(form2.get('string') === 'string-value');
assert.ok(form2.get('missing') === null);

const file2 = form2.get('file');
assert.ok(file2 instanceof File);
assert.ok(file2.name === 'file.txt');
assert.ok(file2.type === 'text/plain');
assert.ok((await file2.text()) === 'contents');
});

test('Request - no `body` for `GET`', () => {
let err: Error | undefined;
try {
new Request('http://example.com', { body: 'bad' });
} catch (_err: any) {
err = _err;
}
assert.ok(err);
assert.equal(err.name, 'TypeError');
assert.equal(err.message, 'Body not allowed for GET or HEAD requests');
});

test.run();
import './canvas';
import './fetch';
import './form-data';
import './switch';
109 changes: 109 additions & 0 deletions apps/tests/src/switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { suite } from 'uvu';
import * as assert from 'uvu/assert';

const test = suite('Switch');

test('`Switch.entrypoint` is a string', () => {
assert.type(Switch.entrypoint, 'string');
assert.ok(Switch.entrypoint.length > 0);
});

test('`Switch.cwd()` is a URL string representation', async () => {
const cwd = Switch.cwd();
assert.type(cwd, 'string');
assert.ok(cwd.startsWith('sdmc:/'));
assert.ok(cwd.endsWith('/'));
});

test('`Switch.readDirSync()` works on relative path', () => {
const files = Switch.readDirSync('.');
assert.ok(Array.isArray(files));
assert.ok(files.length > 0);
});

test('`Switch.readDirSync()` works on `sdmc:/` path', () => {
const files = Switch.readDirSync('sdmc:/');
assert.ok(Array.isArray(files));
assert.ok(files.length > 0);
});

test('`Switch.readDirSync()` works on `romfs:/` path', () => {
const files = Switch.readDirSync('romfs:/');
assert.ok(Array.isArray(files));
assert.ok(files.length > 0);
});

test('`Switch.readDirSync()` throws error when directory does not exist', () => {
let err: Error | undefined;
try {
Switch.readDirSync('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.resolveDns()` works', async () => {
const result = await Switch.resolveDns('n8.io');
assert.ok(Array.isArray(result));
assert.ok(result.length > 0);
});

test('`Switch.resolveDns()` rejects with invalid hostname', async () => {
let err: Error | undefined;
try {
await Switch.resolveDns('example-that-definitely-does-not-exist.nxjs');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.readFile()` works with string path', async () => {
const data = await Switch.readFile('romfs:/runtime.js');
assert.ok(data instanceof ArrayBuffer);
assert.ok(data.byteLength > 0);
});

test('`Switch.readFile()` works with URL path', async () => {
const data = await Switch.readFile(new URL(Switch.entrypoint));
assert.ok(data instanceof ArrayBuffer);
assert.ok(data.byteLength > 0);
});

test('`Switch.readFile()` rejects when file does not exist', async () => {
let err: Error | undefined;
try {
await Switch.readFile('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.readFile()` rejects when attempting to read a directory', async () => {
let err: Error | undefined;
try {
await Switch.readFile('.');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test('`Switch.stat()` returns file information', async () => {
const stat = await Switch.stat(Switch.entrypoint);
assert.ok(stat.size > 0);
});

test('`Switch.stat()` rejects when file does not exist', async () => {
let err: Error | undefined;
try {
await Switch.stat('romfs:/__does_not_exist__');
} catch (_err) {
err = _err as Error;
}
assert.ok(err);
});

test.run();
4 changes: 2 additions & 2 deletions apps/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2020",
"moduleResolution": "NodeNext",
"target": "ES2020",
"moduleResolution": "Bundler",
"noEmit": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down

0 comments on commit 1536526

Please sign in to comment.