-
Notifications
You must be signed in to change notification settings - Fork 5
/
test-src.js
51 lines (43 loc) · 1.74 KB
/
test-src.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import {default as SQL, join} from "./";
import assert from "assert";
describe("pg-template-tag", function() {
it("collects values and write placeholders", function() {
var literal = SQL`foo ${123} ${"abc"} bar ${null}`;
assert.equal(literal.text, "foo $1 $2 bar $3");
assert.deepEqual(literal.values, [123, "abc", null]);
});
it("accepts nested literals", function() {
var literal = SQL`foo ${SQL`bar ${1} ${2} ${SQL`baz`}`} ${SQL`foobar ${3}`} ${4}`;
assert.equal(literal.text, "foo bar $1 $2 baz foobar $3 $4");
assert.deepEqual(literal.values, [1, 2, 3, 4]);
});
it("does not interpolate arrays into values array", function() {
var literal = SQL`${[1, 2, 3]}`;
assert.deepEqual(literal.values, [[1, 2, 3]]);
});
it("joins SQL instances", function () {
var literal = join([SQL`foo`, SQL`bar ${123}`, SQL`${456}`], " sep ");
assert.equal(literal.text, "foo sep bar $1 sep $2");
assert.deepEqual(literal.values, [123, 456]);
});
it("joins with default comma", function () {
var literal = join([SQL`foo`, SQL`bar`]);
assert.equal(literal.text, "foo,bar");
});
it("joins other types", function () {
var literal = join([1, 'hello', [1, 2, 3]]);
assert.equal(literal.text, "$1,$2,$3");
assert.deepEqual(literal.values, [1, 'hello', [1, 2, 3]]);
});
it("reuses values for reused child parts", function () {
var child = SQL`${0}`;
var literal = SQL`${child} ${child} ${child}`;
assert.equal(literal.text, '$1 $1 $1');
assert.deepEqual(literal.values, [0]);
});
it("does not reuse values if child parts aren't reused", function() {
var literal = SQL`${0} ${0} ${0}`;
assert.equal(literal.text, '$1 $2 $3');
assert.deepEqual(literal.values, [0, 0, 0]);
});
});