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

Loose mode test, idk if I already had one of these or not #1122

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
26 changes: 26 additions & 0 deletions test-app/app/helpers/clock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { cell, resource, resourceFactory } from 'ember-resources';

export function Clock() {
let time = cell(new Date());

return resource(({ on }) => {
let interval = setInterval(() => (time.current = new Date()), 1000);

on.cleanup(() => clearInterval(interval));

// this works at runtime but TS/Glint can't figure it out
// return time;

// The above is a shorthand for this
return () => time.current as Date & { toHTML(): string };
// this cast is a Glint Hack
// because Glint things Date can't be rendered...
});
}

// template-usage compatibility
// (if we only invoke fthe resource function from JS, we don't need this)
resourceFactory(Clock);

// Globals-resolver compatibility
export default Clock;
11 changes: 11 additions & 0 deletions test-app/app/helpers/time-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const locale = 'en-US';
const formatter = new Intl.DateTimeFormat(locale, {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false,
});

export default function timeFormat(input: Date) {
return formatter.format(input);
}
39 changes: 39 additions & 0 deletions test-app/tests/loose-mode-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { find, render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';

module('Loose mode', function (hooks) {
setupRenderingTest(hooks);

test('it works', async function (assert) {
await render(hbs`
{{! template-lint-disable no-curly-component-invocation }}
<time>{{ clock }}</time>
`);

assert.dom('time').hasAnyText();

let initialText = find('time')?.innerText?.trim();

await new Promise((resolve) => setTimeout(resolve, 1100));

assert.ok(initialText);
assert.notStrictEqual(find('time')?.innerText?.trim(), initialText);
});

test('can be passed to a helper', async function (assert) {
await render(hbs`
<time>{{time-format (clock) }}</time>
`);

assert.dom('time').hasAnyText();

let initialText = find('time')?.innerText?.trim();

await new Promise((resolve) => setTimeout(resolve, 1100));

assert.ok(initialText);
assert.notStrictEqual(find('time')?.innerText?.trim(), initialText);
});
});
4 changes: 4 additions & 0 deletions test-app/types/glint-registry.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ import '@glint/environment-ember-loose';
import '@glint/environment-ember-loose/native-integration';

// import type { ComponentLike, HelperLike, ModifierLike } from "@glint/template";
import type Clock from 'test-app/helpers/clock';
import type TimeFormat from 'test-app/helpers/time-format';

declare module '@glint/environment-ember-loose/registry' {
export default interface Registry {
clock: typeof Clock;
'time-format': typeof TimeFormat;
// Examples
// state: HelperLike<{ Args: {}, Return: State }>;
// attachShadow: ModifierLike<{ Args: { Positional: [State['update']]}}>;
Expand Down
Loading