Skip to content

Commit 91dd7d5

Browse files
authored
chore: fix tests (#438)
* refactor: fix tests * fix: fix test not cleaning up
1 parent b604127 commit 91dd7d5

File tree

2 files changed

+82
-51
lines changed

2 files changed

+82
-51
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import db, { sequencerDB } from '../../../src/helpers/mysql';
2+
import * as utils from '../../../src/helpers/utils';
3+
import { action } from '../../../src/writer/profile';
4+
5+
afterEach(() => {
6+
jest.restoreAllMocks();
7+
});
8+
9+
describe('writer/profile', () => {
10+
afterAll(async () => {
11+
await db.queryAsync('DELETE FROM users');
12+
await db.endAsync();
13+
await sequencerDB.endAsync();
14+
});
15+
16+
describe('action()', () => {
17+
const userProfile = {
18+
name: 'Test name',
19+
avatar: 'https://snapshot.org',
20+
about: 'Bio',
21+
twitter: '',
22+
github: ''
23+
};
24+
25+
beforeEach(async () => {
26+
await db.queryAsync('INSERT INTO users SET ?', [
27+
{ id: '0x0', ipfs: '0', created: 0, profile: JSON.stringify(userProfile) }
28+
]);
29+
});
30+
31+
afterEach(async () => {
32+
await db.queryAsync('DELETE FROM users WHERE id = ?', ['0x0']);
33+
});
34+
35+
it('clears the stamp cache if the avatar has changed', async () => {
36+
const spy = jest.spyOn(utils, 'clearStampCache');
37+
38+
await action(
39+
{
40+
profile: JSON.stringify({ avatar: 'https://newurl.com', name: userProfile.name }),
41+
timestamp: 0,
42+
from: '0x0'
43+
},
44+
45+
'1'
46+
);
47+
48+
return expect(spy).toHaveBeenCalledTimes(1);
49+
});
50+
51+
it('clears the stamp cache if the name has changed', async () => {
52+
const spy = jest.spyOn(utils, 'clearStampCache');
53+
54+
await action(
55+
{
56+
profile: JSON.stringify({ avatar: userProfile.avatar, name: 'New name' }),
57+
timestamp: 0,
58+
from: '0x0'
59+
},
60+
61+
'1'
62+
);
63+
64+
return expect(spy).toHaveBeenCalledTimes(1);
65+
});
66+
67+
it('does not clear the stamp cache if the avatar nor the name has changed', () => {
68+
const spy = jest.spyOn(utils, 'clearStampCache');
69+
70+
action(
71+
{
72+
profile: JSON.stringify({ avatar: userProfile.avatar, name: userProfile.name }),
73+
timestamp: 0,
74+
from: '0x0'
75+
},
76+
'2'
77+
);
78+
79+
return expect(spy).not.toHaveBeenCalled();
80+
});
81+
});
82+
});

test/unit/writer/profile.test.ts

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,5 @@
1-
import db from '../../../src/helpers/mysql';
2-
import { action } from '../../../src/writer/profile';
3-
import * as utils from '../../../src/helpers/utils';
4-
5-
afterEach(() => {
6-
jest.restoreAllMocks();
7-
});
8-
91
describe('writer/profile', () => {
102
describe('verify()', () => {
113
it.todo('rejects if the schema is invalid');
124
});
13-
14-
describe('action()', () => {
15-
const userProfile = {
16-
name: 'Test name',
17-
avatar: 'https://snapshot.org',
18-
about: 'Bio',
19-
twitter: '',
20-
github: ''
21-
};
22-
23-
beforeAll(async () => {
24-
await db.queryAsync('INSERT INTO users SET ?', [
25-
{ id: '0x0', ipfs: '0', created: 0, profile: JSON.stringify(userProfile) }
26-
]);
27-
});
28-
29-
afterAll(async () => {
30-
await db.queryAsync('DELETE FROM users WHERE id = ?', ['0x0']);
31-
});
32-
33-
it('clears the stamp cache if the avatar has changed', async () => {
34-
const spy = jest.spyOn(utils, 'clearStampCache');
35-
36-
await action(
37-
{ profile: JSON.stringify({ avatar: 'https://newurl.com' }), timestamp: 0, from: '0x0' },
38-
39-
'1'
40-
);
41-
42-
return expect(spy).toHaveBeenCalledTimes(2);
43-
});
44-
45-
it('does not clear the stamp cache if the avatar has not changed', () => {
46-
const spy = jest.spyOn(utils, 'clearStampCache');
47-
48-
action(
49-
{ profile: JSON.stringify({ avatar: userProfile.avatar }), timestamp: 0, from: '0x0' },
50-
'2'
51-
);
52-
53-
return expect(spy).not.toHaveBeenCalled();
54-
});
55-
});
565
});

0 commit comments

Comments
 (0)