Socialify

Folder ..

Viewing import.test.js
183 lines (161 loc) • 4.9 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import GDrive from 'main/application/sync/gdrive'
import AuthWindow from 'main/window/auth'
import { google } from 'googleapis'

const mockCloseAuth = jest.fn()
const mockRemoveMenu = jest.fn()
jest.mock('main/window/auth', () => {
  return jest.fn().mockImplementation(() => {
    return {
      removeMenu: mockRemoveMenu,
      authenticate: jest.fn(() => Promise.resolve('AUTH_CODE')),
      close: mockCloseAuth
    }
  })
})

const vault = {
  write: jest.fn()
}

describe('#import', () => {
  let sync, folderExistsMock, fileExistsMock, fileContentMock
  const drive = {
    files: {
      get: jest.fn(() => fileContentMock()),
      list: jest.fn(options => {
        if (options.q === "name = 'Swifty' and trashed = false") {
          return folderExistsMock()
        } else {
          return fileExistsMock()
        }
      }),
      create: jest.fn()
    }
  }
  google.drive = jest.fn(() => drive)

  beforeEach(() => {
    fileContentMock = () => Promise.resolve({ data: 'VAULT DATA' })
    folderExistsMock = () =>
      Promise.resolve({ data: { files: [{ id: 'asdfg' }] } })
    fileExistsMock = () =>
      Promise.resolve({ data: { files: [{ id: 'qwerty' }] } })
    sync = new GDrive()
    sync.initialize(vault, {})
  })

  afterEach(() => {
    jest.clearAllMocks()
  })

  test('opens auth window with auth url', () => {
    return sync.import().then(() => {
      expect(AuthWindow).toHaveBeenCalledWith(
        'https://example.com/google_oauth2'
      )
    })
  })

  test('removes menu from auth window', () => {
    return sync.import().then(() => {
      expect(mockRemoveMenu).toHaveBeenCalledTimes(1)
    })
  })

  test('closes auth window on authentication done', () => {
    return sync.import().then(() => {
      expect(mockCloseAuth).toHaveBeenCalledTimes(1)
    })
  })

  test('calls get token with auth code', () => {
    return sync.import().then(() => {
      expect(sync.client.auth.getToken).toHaveBeenCalledWith('AUTH_CODE')
    })
  })

  test('initializes drive client', () => {
    return sync.import().then(() => {
      expect(google.drive).toHaveBeenCalledWith({
        version: 'v3',
        auth: sync.client.auth
      })
    })
  })

  describe('folder does not exist', () => {
    beforeEach(() => {
      folderExistsMock = () => Promise.resolve({})
    })

    test('checks for presence of swifty folder', () => {
      return sync.import().then(() => {
        expect(drive.files.list).toHaveBeenCalledWith({
          q: "name = 'Swifty' and trashed = false",
          fields: 'files(id, name)'
        })
      })
    })

    test('does not create swifty folder', () => {
      return sync.import().then(() => {
        expect(drive.files.create).not.toHaveBeenCalled()
      })
    })

    test('does not write data to vault', () => {
      return sync.import().then(() => {
        expect(vault.write).not.toHaveBeenCalled()
      })
    })

    test('resolves with null result', () => {
      return sync.import().then(result => expect(result).toEqual(null))
    })
  })

  describe('folder exists', () => {
    describe('file does not exist', () => {
      beforeEach(() => {
        fileExistsMock = () => Promise.resolve({})
      })

      test('checks for presence of swifty folder', () => {
        return sync.import().then(() => {
          expect(drive.files.list).toHaveBeenCalledWith({
            q: "name = 'Swifty' and trashed = false",
            fields: 'files(id, name)'
          })
        })
      })

      test('checks for presence of vault file', () => {
        return sync.import().then(() => {
          expect(drive.files.list).toHaveBeenCalledWith({
            q: "name = 'vault.swftx' and 'asdfg' in parents",
            fields: 'files(id, name)'
          })
        })
      })

      test('does not read vault file', () => {
        return sync.import().then(() => {
          expect(drive.files.get).not.toHaveBeenCalled()
        })
      })
    })
    describe('file exists', () => {
      test('checks for presence of swifty folder', () => {
        return sync.import().then(() => {
          expect(drive.files.list).toHaveBeenCalledWith({
            q: "name = 'Swifty' and trashed = false",
            fields: 'files(id, name)'
          })
        })
      })

      test('checks for presence of vault file', () => {
        return sync.import().then(() => {
          expect(drive.files.list).toHaveBeenCalledWith({
            q: "name = 'vault.swftx' and 'asdfg' in parents",
            fields: 'files(id, name)'
          })
        })
      })

      test('reads vault file', () => {
        return sync.import().then(() => {
          expect(drive.files.get).toHaveBeenCalledWith({
            fileId: 'qwerty',
            alt: 'media'
          })
        })
      })

      test('resolves with imported data', () => {
        return sync.import().then(result => {
          expect(result).toEqual('VAULT DATA')
        })
      })
    })
  })
})