Skip to content

Commit f7c3e33

Browse files
committed
add jobs resolver tests
1 parent 55bf931 commit f7c3e33

File tree

7 files changed

+163
-10
lines changed

7 files changed

+163
-10
lines changed

tests/fixtures/item/27414470.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"by": "yiggydyang",
3+
"id": 27414470,
4+
"score": 1,
5+
"time": 1622998836,
6+
"title": "PocketSuite Is hiring Lead Engineers, Product Designers to enrich small business",
7+
"type": "job",
8+
"url": "https://www.ycombinator.com/companies/pocketsuite"
9+
}

tests/fixtures/item/27421523.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"by": "ifc",
3+
"id": 27421523,
4+
"score": 1,
5+
"time": 1623067272,
6+
"title": "Qventus (YC W15) Is Hiring Data Platform Engineers",
7+
"type": "job",
8+
"url": "https://jobs.lever.co/qventus/c0d604cf-ec1c-48ea-847d-5ffc7a67698d"
9+
}

tests/fixtures/jobstories.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[27421523, 27414470]

tests/lib/cache/RedisCache.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,28 +14,34 @@ afterEach(() => {
1414
});
1515

1616
it('adds items to the cache', async () => {
17-
expect(await cache.has('one')).toBeFalsy();
17+
const key = 'one-' + (Math.random() * 10000).toString();
1818

19-
cache.put('one', 1, 10);
19+
expect(await cache.has(key)).toBeFalsy();
2020

21-
expect(await cache.get('one')).toBe(1);
21+
cache.put(key, 1, 10);
22+
23+
expect(await cache.get(key)).toBe(1);
2224
});
2325

2426
it('returns an item in the cache', async () => {
25-
cache.put('one', 1, 10);
27+
const key = 'one-' + (Math.random() * 10000).toString();
28+
29+
cache.put(key, 1, 10);
2630

27-
expect(await cache.get('one')).toStrictEqual(1);
31+
expect(await cache.get(key)).toStrictEqual(1);
2832
});
2933

3034
it('returns null for an item not in the cache', async () => {
3135
expect(await cache.get('missing')).toBeNull();
3236
});
3337

3438
it('determines if an item is in the cache', async () => {
35-
cache.put('one', 1, 10);
39+
const keys = ['one-' + (Math.random() * 10000).toString(), 'two-' + (Math.random() * 10000).toString()];
40+
41+
cache.put(keys[0], 1, 10);
3642

37-
expect(await cache.has('one')).toBeTruthy();
38-
expect(await cache.has('two')).toBeFalsy();
43+
expect(await cache.has(keys[0])).toBeTruthy();
44+
expect(await cache.has(keys[1])).toBeFalsy();
3945
});
4046

4147
it('prefixes all keys with a prefix', async () => {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`it retrieves each job story and caches it 1`] = `
4+
Object {
5+
"jobstory:27414470": Object {
6+
"expires": 0,
7+
"ts": 0,
8+
"value": Object {
9+
"by": "yiggydyang",
10+
"id": 27414470,
11+
"score": 1,
12+
"time": 1622998836,
13+
"title": "PocketSuite Is hiring Lead Engineers, Product Designers to enrich small business",
14+
"type": "job",
15+
"url": "https://www.ycombinator.com/companies/pocketsuite",
16+
},
17+
},
18+
"jobstory:27421523": Object {
19+
"expires": 0,
20+
"ts": 0,
21+
"value": Object {
22+
"by": "ifc",
23+
"id": 27421523,
24+
"score": 1,
25+
"time": 1623067272,
26+
"title": "Qventus (YC W15) Is Hiring Data Platform Engineers",
27+
"type": "job",
28+
"url": "https://jobs.lever.co/qventus/c0d604cf-ec1c-48ea-847d-5ffc7a67698d",
29+
},
30+
},
31+
"jobstoryids:2:d102c7c5187b3dadeb7b29555a5d74d28a8cd87d": Object {
32+
"expires": 0,
33+
"ts": 0,
34+
"value": Array [
35+
27421523,
36+
27414470,
37+
],
38+
},
39+
}
40+
`;
41+
42+
exports[`it retrieves the story data and returns it 1`] = `
43+
Array [
44+
Object {
45+
"by": "ifc",
46+
"expires": 0,
47+
"id": 27421523,
48+
"score": 1,
49+
"time": 1623067272,
50+
"title": "Qventus (YC W15) Is Hiring Data Platform Engineers",
51+
"type": "job",
52+
"url": "https://jobs.lever.co/qventus/c0d604cf-ec1c-48ea-847d-5ffc7a67698d",
53+
},
54+
Object {
55+
"by": "yiggydyang",
56+
"expires": 0,
57+
"id": 27414470,
58+
"score": 1,
59+
"time": 1622998836,
60+
"title": "PocketSuite Is hiring Lead Engineers, Product Designers to enrich small business",
61+
"type": "job",
62+
"url": "https://www.ycombinator.com/companies/pocketsuite",
63+
},
64+
]
65+
`;

tests/resolvers/jobs.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* eslint-disable no-undef */
2+
3+
import { MemoryCache } from '@/lib/cache/MemoryCache';
4+
import { getResolver } from '@/resolvers/jobs';
5+
const hash = require('object-hash');
6+
7+
let cache: MemoryCache;
8+
let resolver: any;
9+
const storyIds: number[] = [];
10+
const stories: Record<string, any>[] = [];
11+
12+
process.env.HACKERNEWS_API_URL = 'http://127.0.0.1:3031';
13+
14+
beforeEach(() => {
15+
cache = new MemoryCache();
16+
resolver = getResolver(storyIds, stories, cache);
17+
});
18+
19+
it('it retrieves the story data and returns it', async () => {
20+
const req = { body: { first: 2, skipText: false } };
21+
const data = await resolver({}, { first: 2, skipText: false }, { req });
22+
23+
for (const item of data) {
24+
item.expires = 0;
25+
}
26+
27+
expect(data).toMatchSnapshot();
28+
});
29+
30+
it('it retrieves the job ids and caches them', async () => {
31+
const req = { body: { query: 'abc', vars: 'bbb' } };
32+
const reqHash = hash(req.body);
33+
34+
await resolver({}, { first: 2, skipText: false }, { req });
35+
36+
expect(await cache.has(`jobstoryids:2:${reqHash}`)).toBeTruthy();
37+
});
38+
39+
it('it retrieves each job story and caches it', async () => {
40+
const req = { body: { first: 2, skipText: false, a: 1, b: 2 } };
41+
const data = await resolver({}, { first: 2, skipText: false }, { req });
42+
43+
data.forEach(item => {
44+
expect(cache.has(`jobstory:${item.id}`)).toBeTruthy();
45+
});
46+
47+
for (const item in cache.map) {
48+
cache.map[item].ts = 0;
49+
cache.map[item].expires = 0;
50+
}
51+
52+
expect(cache.map).toMatchSnapshot();
53+
});

tests/server/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,21 @@ import { logger } from '@tinyhttp/logger';
33
import sirv from 'sirv';
44
import { readFile } from 'fs/promises';
55

6+
const loadFixtureFile = async filename => {
7+
const file = await readFile(`${process.cwd()}/tests/fixtures/${filename}`);
8+
9+
return file.toString();
10+
};
11+
612
new App()
713
.use(logger())
814
.get('/newstories.json', async (_, res) => {
9-
const file = await readFile(`${process.cwd()}/tests/fixtures/newstories.json`);
10-
res.send(file.toString());
15+
const data = await loadFixtureFile('newstories.json');
16+
res.send(data);
17+
})
18+
.get('/jobstories.json', async (_, res) => {
19+
const data = await loadFixtureFile('jobstories.json');
20+
res.send(data);
1121
})
1222
.use(sirv('./tests/fixtures'))
1323
.listen(3031, () => console.log('listening on http://localhost:3031'));

0 commit comments

Comments
 (0)