Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F10352714
test.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
3 KB
Referenced Files
None
Subscribers
None
test.ts
View Options
import
assert
from
"assert"
;
import
{
Readable
}
from
"stream"
;
import
{
promisify
}
from
"util"
;
import
{
Templatable
,
tempstream
}
from
"."
;
import
{
Slot
}
from
"./slot"
;
import
streamToString
from
"./tostring"
;
import
sinon
from
"sinon"
;
const
st
=
(
time
:
number
,
cb
:
()
=>
void
)
=>
setTimeout
(
cb
,
time
);
const
sleep
=
promisify
(
st
);
// a test
function
html
(
strings
:
TemplateStringsArray
,
...
params
:
Templatable
[]
)
:
Readable
{
const
title
=
new
Slot
(
"Default page title"
);
setTimeout
(()
=>
title
.
set
(
"Changed page title"
),
3000
);
console
.
error
(
"try playing around with the above timeout. Weird things happen when you go to e.g. 3000"
);
// process.exit(1);
return
tempstream
/* HTML */
`<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>
${
title
}
</title>
</head>
<body>
${
tempstream
(
strings
,
...
params
)
}
</body> `
;
}
// template`hello ${world}, and ${name}`.pipe(process.stdout);
describe
(
"tempstream"
,
()
=>
{
it
(
"renders properly in the basic case"
,
async
()
=>
{
const
list_items
=
Promise
.
resolve
(
[
"one"
,
"two"
,
"three"
].
map
((
e
)
=>
`<li>
${
e
}
</li>`
)
);
const
title
=
new
Slot
(
"Default page title"
);
setTimeout
(()
=>
title
.
set
(
"Changed page title"
),
20
);
const
slept
=
sleep
(
100
).
then
(()
=>
"slept"
);
const
result
=
await
streamToString
(
tempstream
`<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>
${
title
}
</title>
</head>
<body>
hello World, I
${
slept
}
.
<ul>
${
list_items
}
</ul>
</body> `
);
assert
.
strictEqual
(
result
,
`<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<title>Changed page title</title>
</head>
<body>
hello World, I slept.
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
</body> `
);
});
it
(
"doesn't add unecessary 'null' when encountering an unresolved slot"
,
async
()
=>
{
const
title
=
new
Slot
(
"unchanged"
);
const
result
=
await
streamToString
(
tempstream
`<title>
${
title
}
</title>`
);
assert
.
strictEqual
(
result
,
`<title>unchanged</title>`
);
});
it
(
"handles stream within a stream"
,
async
()
=>
{
const
item_elements
=
Promise
.
resolve
([
"one"
,
"two"
,
"three"
]);
const
inside_stream
=
tempstream
`Here's a list:
${
item_elements
}
`
;
const
outside_stream
=
tempstream
`<div>
${
inside_stream
}
</div>`
;
const
result
=
await
streamToString
(
outside_stream
);
assert
.
strictEqual
(
result
,
`<div>Here's a list: one
two
three</div>`
);
});
it
(
"warns about unset slots"
,
async
()
=>
{
const
console_warn
=
sinon
.
spy
(
console
,
"warn"
);
const
title
=
new
Slot
(
"unchanged"
);
const
result
=
await
streamToString
(
tempstream
`<title>
${
title
}
</title>`
);
assert
.
strictEqual
(
result
,
`<title>unchanged</title>`
);
assert
.
strictEqual
(
console_warn
.
callCount
,
1
);
console_warn
.
restore
();
});
it
(
"handles an array of promises"
,
async
()
=>
{
async
function
process
(
text
:
string
)
{
return
"processed "
+
text
;
}
const
result
=
await
streamToString
(
tempstream
`
${
[
"a"
,
"b"
,
"c"
].
map
(
process
)
}
`
);
assert
.
strictEqual
(
result
,
`processed a
processed b
processed c`
);
});
it
(
"handles an array of promises of streams"
,
async
()
=>
{
async
function
process
(
text
:
string
)
{
return
tempstream
`processed
${
text
}
`
;
}
const
result
=
await
streamToString
(
tempstream
`
${
[
"a"
,
"b"
,
"c"
].
map
(
process
)
}
`
);
assert
.
strictEqual
(
result
,
`processed a
processed b
processed c`
);
});
});
File Metadata
Details
Attached
Mime Type
text/html
Expires
Sun, Nov 2, 18:21 (1 d, 8 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1030480
Default Alt Text
test.ts (3 KB)
Attached To
Mode
rSTREAM tempstream
Attached
Detach File
Event Timeline
Log In to Comment