-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathsitemaps.ts
103 lines (97 loc) · 2.65 KB
/
sitemaps.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
import { FastifyInstance } from 'fastify';
import appFunc from '../src';
import request from 'supertest';
import nock from 'nock';
import { saveFixtures } from './helpers';
import { DataSource, DeepPartial } from 'typeorm';
import createOrGetConnection from '../src/db';
import { Keyword, Post, PostType, Source } from '../src/entity';
import { sourcesFixture } from './fixture/source';
import { keywordsFixture } from './fixture/keywords';
let app: FastifyInstance;
let con: DataSource;
const now = new Date();
const postsFixture: DeepPartial<Post>[] = [
{
id: 'p1',
shortId: 'p1',
title: 'P1',
sourceId: 'a',
createdAt: now,
type: PostType.Article,
upvotes: 20,
},
{
id: 'p2',
shortId: 'p2',
title: 'P2',
sourceId: 'b',
banned: true,
createdAt: new Date(now.getTime() - 1000),
type: PostType.Article,
},
{
id: 'p3',
shortId: 'p3',
title: 'P3',
sourceId: 'c',
private: true,
createdAt: new Date(now.getTime() - 2000),
type: PostType.Collection,
},
{
id: 'p4',
shortId: 'p4',
title: 'P4',
sourceId: 'a',
createdAt: new Date(now.getTime() - 3000),
type: PostType.Article,
upvotes: 80,
},
{
id: 'p5',
shortId: 'p5',
title: 'P5',
sourceId: 'b',
createdAt: new Date(now.getTime() - 4000),
type: PostType.Collection,
},
];
beforeAll(async () => {
con = await createOrGetConnection();
app = await appFunc();
return app.ready();
});
beforeEach(async () => {
nock.cleanAll();
await saveFixtures(con, Keyword, keywordsFixture);
await saveFixtures(con, Source, sourcesFixture);
await con.getRepository(Post).insert(postsFixture);
});
afterAll(() => app.close());
describe('GET /sitemaps/posts.txt', () => {
it('should return posts ordered by time', async () => {
const res = await request(app.server)
.get('/sitemaps/posts.txt')
.expect(200);
expect(res.header['content-type']).toEqual('text/plain');
expect(res.header['cache-control']).toBeTruthy();
expect(res.text).toEqual(`http://localhost:5002/posts/p1-p1
http://localhost:5002/posts/p4-p4
http://localhost:5002/posts/p5-p5
`);
});
});
describe('GET /sitemaps/tags.txt', () => {
it('should return tags ordered alphabetically', async () => {
const res = await request(app.server).get('/sitemaps/tags.txt').expect(200);
expect(res.header['content-type']).toEqual('text/plain');
expect(res.header['cache-control']).toBeTruthy();
expect(res.text).toEqual(`http://localhost:5002/tags/development
http://localhost:5002/tags/fullstack
http://localhost:5002/tags/golang
http://localhost:5002/tags/rust
http://localhost:5002/tags/webdev
`);
});
});