Skip to content

Commit

Permalink
Cherry picking refactored fb.js from hoppscotch#879 by @AndrewBastin (h…
Browse files Browse the repository at this point in the history
…oppscotch#1286)

* Cherry picking refactored fb.js from hoppscotch#879 by @AndrewBastin

* Fixed a minor UI glitch in History section

* Removed logout success toast testcase

Co-authored-by: Andrew Bastin <[email protected]>
  • Loading branch information
liyasthomas and AndrewBastin authored Oct 17, 2020
1 parent b6b3cbc commit 22a3bba
Show file tree
Hide file tree
Showing 13 changed files with 2,405 additions and 470 deletions.
1 change: 1 addition & 0 deletions __mocks__/svgMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
1 change: 1 addition & 0 deletions assets/scss/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ hr {

button {
@apply justify-start;
@apply text-left;
}
}

Expand Down
133 changes: 133 additions & 0 deletions components/firebase/__tests__/feeds.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import feeds from "../feeds"
import { shallowMount } from "@vue/test-utils"

jest.mock("~/helpers/fb", () => {
return {
__esModule: true,
fb: {
currentFeeds: [
{
id: "test1",
label: "First",
message: "First Message",
},
{
id: "test2",
label: "Second",
},
{
id: "test3",
message: "Third Message",
},
{
id: "test4",
},
],
deleteFeed: jest.fn(() => Promise.resolve()),
},
}
})

const { fb } = require("~/helpers/fb")

const factory = () =>
shallowMount(feeds, {
mocks: {
$t: (text) => text,
$toast: {
error: jest.fn(),
},
},
})

beforeEach(() => {
fb.deleteFeed.mockClear()
})

describe("feeds", () => {
test("mounts properly when proper components are given", () => {
const wrapper = factory()

expect(wrapper).toBeTruthy()
})

test("renders all the current feeds", () => {
const wrapper = factory()

expect(wrapper.findAll("div[data-test='list-item']").wrappers).toHaveLength(4)
})

test("feeds with no label displays the 'no_label' message", () => {
const wrapper = factory()

expect(
wrapper
.findAll("label[data-test='list-label']")
.wrappers.map((e) => e.text())
.filter((text) => text == "no_label")
).toHaveLength(2)
})

test("feeds with no message displays the 'empty' message", () => {
const wrapper = factory()

expect(
wrapper
.findAll("li[data-test='list-message']")
.wrappers.map((e) => e.text())
.filter((text) => text == "empty")
).toHaveLength(2)
})

test("labels in the list are proper", () => {
const wrapper = factory()

expect(wrapper.findAll("label[data-test='list-label']").wrappers.map((e) => e.text())).toEqual([
"First",
"Second",
"no_label",
"no_label",
])
})

test("messages in the list are proper", () => {
const wrapper = factory()

expect(wrapper.findAll("li[data-test='list-message']").wrappers.map((e) => e.text())).toEqual([
"First Message",
"empty",
"Third Message",
"empty",
])
})

test("clicking on the delete button deletes the feed", async () => {
const wrapper = factory()

const deleteButton = wrapper.find("button")

await deleteButton.trigger("click")

expect(fb.deleteFeed).toHaveBeenCalledTimes(1)
})

test("correct feed is passed to from the list for deletion", async () => {
const wrapper = factory()

const deleteButton = wrapper.find("button")

await deleteButton.trigger("click")

expect(fb.deleteFeed).toHaveBeenCalledWith("test1")
})

test("renders the 'empty' label if no elements in the current feeds", () => {
jest.spyOn(fb, "currentFeeds", "get").mockReturnValueOnce([])

const wrapper = factory()

expect(wrapper.findAll("li").wrappers).toHaveLength(1)

expect(wrapper.find("li").text()).toEqual("empty")
})
})
93 changes: 93 additions & 0 deletions components/firebase/__tests__/inputform.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import inputform from "../inputform"
import { shallowMount } from "@vue/test-utils"

jest.mock("~/helpers/fb", () => {
return {
__esModule: true,
fb: {
writeFeeds: jest.fn(() => Promise.resolve()),
},
}
})

const { fb } = require("~/helpers/fb")

const factory = () =>
shallowMount(inputform, {
mocks: {
$t: (text) => text,
},
})

beforeEach(() => {
fb.writeFeeds.mockClear()
})

describe("inputform", () => {
test("mounts properly", () => {
const wrapper = factory()

expect(wrapper).toBeTruthy()
})
test("calls writeFeeds when submitted properly", async () => {
const wrapper = factory()

const addButton = wrapper.find("button")
const [messageInput, labelInput] = wrapper.findAll("input").wrappers

await messageInput.setValue("test message")
await labelInput.setValue("test label")

await addButton.trigger("click")

expect(fb.writeFeeds).toHaveBeenCalledTimes(1)
})
test("doesn't call writeFeeds when submitted without a data", async () => {
const wrapper = factory()

const addButton = wrapper.find("button")

await addButton.trigger("click")

expect(fb.writeFeeds).not.toHaveBeenCalled()
})
test("doesn't call writeFeeds when message or label is null", async () => {
const wrapper = factory()

const addButton = wrapper.find("button")
const [messageInput, labelInput] = wrapper.findAll("input").wrappers

await messageInput.setValue(null)
await labelInput.setValue(null)

await addButton.trigger("click")

expect(fb.writeFeeds).not.toHaveBeenCalled()
})
test("doesn't call writeFeeds when message or label is empty", async () => {
const wrapper = factory()

const addButton = wrapper.find("button")
const [messageInput, labelInput] = wrapper.findAll("input").wrappers

await messageInput.setValue("")
await labelInput.setValue("")

await addButton.trigger("click")

expect(fb.writeFeeds).not.toHaveBeenCalled()
})
test("calls writeFeeds with correct values", async () => {
const wrapper = factory()

const addButton = wrapper.find("button")
const [messageInput, labelInput] = wrapper.findAll("input").wrappers

await messageInput.setValue("test message")
await labelInput.setValue("test label")

await addButton.trigger("click")

expect(fb.writeFeeds).toHaveBeenCalledWith("test message", "test label")
})
})
66 changes: 66 additions & 0 deletions components/firebase/__tests__/logout.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import logout from "../logout"
import { shallowMount, createLocalVue } from "@vue/test-utils"

jest.mock("~/helpers/fb", () => {
return {
__esModule: true,
fb: {
signOutUser: jest.fn(() => Promise.resolve()),
},
}
})

const { fb } = require("~/helpers/fb")

const $toast = {
info: jest.fn(),
show: jest.fn(),
}

const localVue = createLocalVue()

localVue.directive("close-popover", {})

const factory = () =>
shallowMount(logout, {
mocks: {
$t: (text) => text,
$toast,
},
localVue,
})

beforeEach(() => {
fb.signOutUser.mockClear()
$toast.info.mockClear()
$toast.show.mockClear()
})

describe("logout", () => {
test("mounts properly", () => {
const wrapper = factory()

expect(wrapper).toBeTruthy()
})

test("clicking the logout button fires the logout firebase function", async () => {
const wrapper = factory()

const button = wrapper.find("button")

await button.trigger("click")

expect(fb.signOutUser).toHaveBeenCalledTimes(1)
})

test("failed signout request fires a error toast", async () => {
fb.signOutUser.mockImplementationOnce(() => Promise.reject("test reject"))

const wrapper = factory()
const button = wrapper.find("button")
await button.trigger("click")

expect($toast.show).toHaveBeenCalledTimes(1)
expect($toast.show).toHaveBeenCalledWith("test reject", expect.anything())
})
})
10 changes: 5 additions & 5 deletions components/firebase/feeds.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
:key="feed.id"
class="flex-col py-2 border-b border-dashed border-brdColor"
>
<div class="show-on-large-screen">
<div data-test="list-item" class="show-on-large-screen">
<li class="info">
<label>
<label data-test="list-label">
{{ feed.label || $t("no_label") }}
</label>
</li>
Expand All @@ -16,7 +16,7 @@
</button>
</div>
<div class="show-on-large-screen">
<li class="info clamb-3">
<li data-test="list-message" class="info clamb-3">
<label>{{ feed.message || $t("empty") }}</label>
</li>
</div>
Expand Down Expand Up @@ -54,8 +54,8 @@ export default {
}
},
methods: {
deleteFeed(feed) {
fb.deleteFeed(feed.id)
async deleteFeed(feed) {
await fb.deleteFeed(feed.id)
this.$toast.error(this.$t("deleted"), {
icon: "delete",
})
Expand Down
Loading

0 comments on commit 22a3bba

Please sign in to comment.