Skip to content

Commit

Permalink
[#26] Create test for crud-sessions.js
Browse files Browse the repository at this point in the history
  • Loading branch information
wayangalihpratama committed Jun 29, 2023
1 parent 13d7377 commit d7601fa
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/src/database/crud/__tests__/crud-sessions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import crudSessions from '../crud-sessions';
jest.mock('expo-sqlite');

describe('crudSessions function', () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe('addSession', () => {
it('should insert the session', async () => {
const result = await crudSessions.addSession({ token: 'Bearer abcefg', passcode: '123456' });
expect(result).toEqual({ rowsAffected: 1 });
});
});

describe('selectLastSession', () => {
it('should return false if session does not exist', async () => {
const result = await crudSessions.selectLastSession();
expect(result).toBe(false);
});

it('should return last session', async () => {
// Mock the result set for select
const sessions = [
{
token: 'Bearer 1',
passcode: '123',
},
{
token: 'Bearer 2',
passcode: '321',
},
];
const mockSelectSql = jest.fn(() => sessions[1]);
crudSessions.selectLastSession = mockSelectSql;
const result = await crudSessions.selectLastSession();
expect(result).toEqual(sessions[1]);
});
});
});

0 comments on commit d7601fa

Please sign in to comment.