Skip to content

Commit

Permalink
fix: user class tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexktzk committed Jun 9, 2019
1 parent 52dd6a2 commit c0bba09
Showing 1 changed file with 128 additions and 106 deletions.
234 changes: 128 additions & 106 deletions test/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,127 +126,149 @@ describe('User class', () => {
exp: 200
};

t.alert = jest.fn(async () => ({}));
t.alert = jest.fn(async () => ({ notified: true }));
storage.getUser = async () => currentUser;
});

describe('when user is gained exp and gold', () => {
describe('when stats notifications are disabled', () => {
beforeAll(() => {
stats = {
gold: 101,
exp: 201
};
storage.getSettings = async () => ({ showStatsNotifications: false });
});

beforeEach(() => {
it('does nothing', async () => {
user = new User(t, storage);
});

it('uses _gained_ word in the message', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching('gained')
})
);
});

it('display notification as success', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
display: 'success'
})
);
});

it('displays how many exp was gained', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(`${stats.exp - currentUser.exp} exp`)
})
);
});

it('displays how many gold was gained', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(
`${(stats.gold - currentUser.gold).toFixed(2)} gold`
)
})
);
});

it('displays the notification for 5 seconds', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
duration: 5
})
);
const result = await user.notifyAboutStats(currentUser);
expect(result).toBe(undefined);
});
});

describe('when user is lost exp and gold', () => {
describe('when stats notifications are enabled', () => {
beforeAll(() => {
stats = {
gold: 99,
exp: 199
};
});

beforeEach(() => {
user = new User(t, storage);
});

it('uses _lost_ word in the message', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching('lost')
})
);
});

it('display notification as error', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
display: 'error'
})
);
});

it('displays how many exp was lost', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(`${stats.exp - currentUser.exp} exp`)
})
);
storage.getSettings = async () => ({ showStatsNotifications: true });
});

it('displays how many gold was lost', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(
`${(stats.gold - currentUser.gold).toFixed(2)} gold`
)
})
);
describe('when user is gained exp and gold', () => {
beforeAll(() => {
stats = {
gold: 101,
exp: 201
};
});

beforeEach(() => {
user = new User(t, storage);
});

it('uses _gained_ word in the message', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching('gained')
})
);
});

it('display notification as success', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
display: 'success'
})
);
});

it('displays how many exp was gained', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(
`${stats.exp - currentUser.exp} exp`
)
})
);
});

it('displays how many gold was gained', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(
`${(stats.gold - currentUser.gold).toFixed(2)} gold`
)
})
);
});

it('displays the notification for 5 seconds', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
duration: 5
})
);
});
});

it('displays the notification for 5 seconds', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
duration: 5
})
);
describe('when user is lost exp and gold', () => {
beforeAll(() => {
stats = {
gold: 99,
exp: 199
};
});

beforeEach(() => {
user = new User(t, storage);
});

it('uses _lost_ word in the message', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching('lost')
})
);
});

it('display notification as error', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
display: 'error'
})
);
});

it('displays how many exp was lost', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(
`${stats.exp - currentUser.exp} exp`
)
})
);
});

it('displays how many gold was lost', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
message: expect.stringMatching(
`${(stats.gold - currentUser.gold).toFixed(2)} gold`
)
})
);
});

it('displays the notification for 5 seconds', async () => {
await user.notifyAboutStats(stats);
expect(user.t.alert).toBeCalledWith(
expect.objectContaining({
duration: 5
})
);
});
});
});
});
Expand Down

0 comments on commit c0bba09

Please sign in to comment.