/
basic.test.ts
133 lines (110 loc) · 3.97 KB
/
basic.test.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { PostgrestClient } from '../src/index'
const REST_URL = 'http://localhost:3000'
const postgrest = new PostgrestClient(REST_URL)
test('basic select table', async () => {
const res = await postgrest.from('users').select()
expect(res).toMatchSnapshot()
})
test('stored procedure', async () => {
const res = await postgrest.rpc('get_status', { name_param: 'supabot' })
expect(res).toMatchSnapshot()
})
test('custom headers', async () => {
const postgrest = new PostgrestClient(REST_URL, { headers: { apikey: 'foo' } })
expect((postgrest.from('users').select() as any).headers['apikey']).toEqual('foo')
})
test('auth', async () => {
const postgrest = new PostgrestClient(REST_URL).auth('foo')
expect((postgrest.from('users').select() as any).headers['Authorization']).toEqual('Bearer foo')
})
test('switch schema', async () => {
const postgrest = new PostgrestClient(REST_URL, { schema: 'personal' })
const res = await postgrest.from('users').select()
expect(res).toMatchSnapshot()
})
test('on_conflict insert', async () => {
const res = await postgrest
.from('users')
.insert({ username: 'dragarcia' }, { upsert: true, onConflict: 'username' })
expect(res).toMatchSnapshot()
})
describe('basic insert, update, delete', () => {
test('basic insert', async () => {
let res = await postgrest
.from('messages')
.insert({ message: 'foo', username: 'supabot', channel_id: 1 })
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('upsert', async () => {
let res = await postgrest
.from('messages')
.insert({ id: 3, message: 'foo', username: 'supabot', channel_id: 2 }, { upsert: true })
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('bulk insert', async () => {
let res = await postgrest.from('messages').insert([
{ message: 'foo', username: 'supabot', channel_id: 1 },
{ message: 'foo', username: 'supabot', channel_id: 1 },
])
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('basic update', async () => {
let res = await postgrest.from('messages').update({ channel_id: 2 }).eq('message', 'foo')
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
test('basic delete', async () => {
let res = await postgrest.from('messages').delete().eq('message', 'foo')
expect(res).toMatchSnapshot()
res = await postgrest.from('messages').select()
expect(res).toMatchSnapshot()
})
})
test('missing table', async () => {
const res = await postgrest.from('missing_table').select()
expect(res).toMatchSnapshot()
})
test('connection error', async () => {
const postgrest = new PostgrestClient('http://this.url.does.not.exist')
let isErrorCaught = false
await postgrest
.from('user')
.select()
.then(undefined, () => {
isErrorCaught = true
})
expect(isErrorCaught).toBe(true)
})
test('custom type', async () => {
interface User {
username: string
data: object | null
age_range: string | null
status: 'ONLINE' | 'OFFLINE'
catchphrase: 'string' | null
}
// TODO: Find a cleaner way to weave a custom type
// eq should show User's properties in LSP/IntelliSense
const { data: users } = <{ data: User[] }>(
await postgrest.from<User>('users').select().eq('username', 'supabot')
)
const user = users[0]
// Autocomplete should show properties of user after '.'
user.username
})
test("don't mutate PostgrestClient.headers", async () => {
await postgrest.from('users').select().limit(1).single()
const { error } = await postgrest.from('users').select()
expect(error).toMatchSnapshot()
})
test("allow ordering on JSON column", async () => {
const { data } = await postgrest.from('users').select().order('data->something')
expect(data).toMatchSnapshot()
})